]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): bind default function of inject to instance (#3925)
authorEunjae Lee <karis612@gmail.com>
Wed, 9 Jun 2021 16:02:05 +0000 (18:02 +0200)
committerGitHub <noreply@github.com>
Wed, 9 Jun 2021 16:02:05 +0000 (12:02 -0400)
fix #3923

packages/runtime-core/__tests__/apiInject.spec.ts
packages/runtime-core/src/apiInject.ts

index f4bf2758fb03d8b02492d511e23d7451bdf56b3e..87a415aa972b42d38058fba444ad3539ea34e80e 100644 (file)
@@ -7,7 +7,8 @@ import {
   nextTick,
   Ref,
   readonly,
-  reactive
+  reactive,
+  defineComponent
 } from '../src/index'
 import { render, nodeOps, serialize } from '@vue/runtime-test'
 
@@ -91,6 +92,34 @@ describe('api: provide/inject', () => {
     expect(serialize(root)).toBe(`<div>foobar</div>`)
   })
 
+  it('bound to instance', () => {
+    const Provider = {
+      setup() {
+        return () => h(Consumer)
+      }
+    }
+
+    const Consumer = defineComponent({
+      name: 'Consumer',
+      inject: {
+        foo: {
+          from: 'foo',
+          default() {
+            return this!.$options.name
+          }
+        }
+      },
+      render() {
+        // @ts-ignore
+        return this.foo
+      }
+    })
+
+    const root = nodeOps.createElement('div')
+    render(h(Provider), root)
+    expect(serialize(root)).toBe(`<div>Consumer</div>`)
+  })
+
   it('nested providers', () => {
     const ProviderOne = {
       setup() {
index a1ec6126be68cf65a6bb3e2132daaf978b102b09..1cf5771c58242ce139eaddb4baa5bf173db7061b 100644 (file)
@@ -60,7 +60,7 @@ export function inject(
       return provides[key as string]
     } else if (arguments.length > 1) {
       return treatDefaultAsFactory && isFunction(defaultValue)
-        ? defaultValue()
+        ? defaultValue.call(instance.proxy)
         : defaultValue
     } else if (__DEV__) {
       warn(`injection "${String(key)}" not found.`)