fix(inject): allow default value to be undefined (#894)

Close #892
This commit is contained in:
Eduardo San Martin Morote 2020-03-30 21:24:55 +02:00 committed by GitHub
parent 573bcb2e11
commit 94562daea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -284,4 +284,27 @@ describe('api: provide/inject', () => {
expect(serialize(root)).toBe(`<div><!----></div>`)
expect(`injection "foo" not found.`).toHaveBeenWarned()
})
it('should not warn when default value is undefined', () => {
const Provider = {
setup() {
return () => h(Middle)
}
}
const Middle = {
render: () => h(Consumer)
}
const Consumer = {
setup() {
const foo = inject('foo', undefined)
return () => foo
}
}
const root = nodeOps.createElement('div')
render(h(Provider), root)
expect(`injection "foo" not found.`).not.toHaveBeenWarned()
})
})

View File

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