Skip to content

Commit d472461

Browse files
committed
fix(inject): fix support for inject option default function
fix #2050
1 parent 6dbc6c4 commit d472461

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

packages/runtime-core/__tests__/apiOptions.spec.ts

+29-36
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe('api: options', () => {
241241
})
242242

243243
test('provide/inject', () => {
244-
const Root = {
244+
const Root = defineComponent({
245245
data() {
246246
return {
247247
a: 1
@@ -253,45 +253,38 @@ describe('api: options', () => {
253253
}
254254
},
255255
render() {
256-
return [h(ChildA), h(ChildB), h(ChildC), h(ChildD)]
257-
}
258-
} as any
259-
const ChildA = {
260-
inject: ['a'],
261-
render() {
262-
return this.a
263-
}
264-
} as any
265-
const ChildB = {
266-
// object alias
267-
inject: { b: 'a' },
268-
render() {
269-
return this.b
256+
return [h(ChildA), h(ChildB), h(ChildC), h(ChildD), h(ChildE)]
270257
}
271-
} as any
272-
const ChildC = {
273-
inject: {
274-
b: {
275-
from: 'a'
258+
})
259+
260+
const defineChild = (injectOptions: any, injectedKey = 'b') =>
261+
({
262+
inject: injectOptions,
263+
render() {
264+
return this[injectedKey]
276265
}
277-
},
278-
render() {
279-
return this.b
266+
} as any)
267+
268+
const ChildA = defineChild(['a'], 'a')
269+
const ChildB = defineChild({ b: 'a' })
270+
const ChildC = defineChild({
271+
b: {
272+
from: 'a'
280273
}
281-
} as any
282-
const ChildD = {
283-
inject: {
284-
b: {
285-
from: 'c',
286-
default: 2
287-
}
288-
},
289-
render() {
290-
return this.b
274+
})
275+
const ChildD = defineChild({
276+
b: {
277+
from: 'c',
278+
default: 2
291279
}
292-
} as any
293-
294-
expect(renderToString(h(Root))).toBe(`1112`)
280+
})
281+
const ChildE = defineChild({
282+
b: {
283+
from: 'c',
284+
default: () => 3
285+
}
286+
})
287+
expect(renderToString(h(Root))).toBe(`11123`)
295288
})
296289

297290
test('lifecycle', async () => {

packages/runtime-core/src/apiInject.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isFunction } from '@vue/shared'
12
import { currentInstance } from './component'
23
import { currentRenderingInstance } from './componentRenderUtils'
34
import { warn } from './warning'
@@ -27,10 +28,15 @@ export function provide<T>(key: InjectionKey<T> | string, value: T) {
2728
}
2829

2930
export function inject<T>(key: InjectionKey<T> | string): T | undefined
30-
export function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
31+
export function inject<T>(
32+
key: InjectionKey<T> | string,
33+
defaultValue: T,
34+
treatDefaultAsFactory?: boolean
35+
): T
3136
export function inject(
3237
key: InjectionKey<any> | string,
33-
defaultValue?: unknown
38+
defaultValue?: unknown,
39+
treatDefaultAsFactory = false
3440
) {
3541
// fallback to `currentRenderingInstance` so that this can be called in
3642
// a functional component
@@ -41,7 +47,9 @@ export function inject(
4147
// TS doesn't allow symbol as index type
4248
return provides[key as string]
4349
} else if (arguments.length > 1) {
44-
return defaultValue
50+
return treatDefaultAsFactory && isFunction(defaultValue)
51+
? defaultValue()
52+
: defaultValue
4553
} else if (__DEV__) {
4654
warn(`injection "${String(key)}" not found.`)
4755
}

packages/runtime-core/src/componentOptions.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,11 @@ export function applyOptions(
457457
for (const key in injectOptions) {
458458
const opt = injectOptions[key]
459459
if (isObject(opt)) {
460-
ctx[key] = inject(opt.from, opt.default)
460+
ctx[key] = inject(
461+
opt.from,
462+
opt.default,
463+
true /* treat default function as factory */
464+
)
461465
} else {
462466
ctx[key] = inject(opt)
463467
}

0 commit comments

Comments
 (0)