Skip to content

Commit 94562da

Browse files
authored
fix(inject): allow default value to be undefined (#894)
Close #892
1 parent 573bcb2 commit 94562da

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,27 @@ describe('api: provide/inject', () => {
284284
expect(serialize(root)).toBe(`<div><!----></div>`)
285285
expect(`injection "foo" not found.`).toHaveBeenWarned()
286286
})
287+
288+
it('should not warn when default value is undefined', () => {
289+
const Provider = {
290+
setup() {
291+
return () => h(Middle)
292+
}
293+
}
294+
295+
const Middle = {
296+
render: () => h(Consumer)
297+
}
298+
299+
const Consumer = {
300+
setup() {
301+
const foo = inject('foo', undefined)
302+
return () => foo
303+
}
304+
}
305+
306+
const root = nodeOps.createElement('div')
307+
render(h(Provider), root)
308+
expect(`injection "foo" not found.`).not.toHaveBeenWarned()
309+
})
287310
})

packages/runtime-core/src/apiInject.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function inject(
4040
if (key in provides) {
4141
// TS doesn't allow symbol as index type
4242
return provides[key as string]
43-
} else if (defaultValue !== undefined) {
43+
} else if (arguments.length > 1) {
4444
return defaultValue
4545
} else if (__DEV__) {
4646
warn(`injection "${String(key)}" not found.`)

0 commit comments

Comments
 (0)