Skip to content

Commit db1dc1c

Browse files
authoredJun 9, 2021
fix(runtime-core): bind default function of inject to instance (#3925)
fix #3923
1 parent bc100c5 commit db1dc1c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
 

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

+30-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
nextTick,
88
Ref,
99
readonly,
10-
reactive
10+
reactive,
11+
defineComponent
1112
} from '../src/index'
1213
import { render, nodeOps, serialize } from '@vue/runtime-test'
1314

@@ -91,6 +92,34 @@ describe('api: provide/inject', () => {
9192
expect(serialize(root)).toBe(`<div>foobar</div>`)
9293
})
9394

95+
it('bound to instance', () => {
96+
const Provider = {
97+
setup() {
98+
return () => h(Consumer)
99+
}
100+
}
101+
102+
const Consumer = defineComponent({
103+
name: 'Consumer',
104+
inject: {
105+
foo: {
106+
from: 'foo',
107+
default() {
108+
return this!.$options.name
109+
}
110+
}
111+
},
112+
render() {
113+
// @ts-ignore
114+
return this.foo
115+
}
116+
})
117+
118+
const root = nodeOps.createElement('div')
119+
render(h(Provider), root)
120+
expect(serialize(root)).toBe(`<div>Consumer</div>`)
121+
})
122+
94123
it('nested providers', () => {
95124
const ProviderOne = {
96125
setup() {

‎packages/runtime-core/src/apiInject.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function inject(
6060
return provides[key as string]
6161
} else if (arguments.length > 1) {
6262
return treatDefaultAsFactory && isFunction(defaultValue)
63-
? defaultValue()
63+
? defaultValue.call(instance.proxy)
6464
: defaultValue
6565
} else if (__DEV__) {
6666
warn(`injection "${String(key)}" not found.`)

0 commit comments

Comments
 (0)