wip: warn when injection not found

This commit is contained in:
Evan You 2019-08-20 15:51:55 -04:00
parent c5558b7ed1
commit 7691c06520

View File

@ -1,4 +1,5 @@
import { currentInstance } from './component' import { currentInstance } from './component'
import { warn } from './warning'
export interface InjectionKey<T> extends Symbol {} export interface InjectionKey<T> extends Symbol {}
@ -29,8 +30,12 @@ export function inject(key: InjectionKey<any> | string, defaultValue?: any) {
} else { } else {
// TODO should also check for app-level provides // TODO should also check for app-level provides
const provides = currentInstance.parent && currentInstance.provides const provides = currentInstance.parent && currentInstance.provides
return provides && key in provides if (provides && key in provides) {
? (provides[key as any] as any) return provides[key as any] as any
: defaultValue } else if (defaultValue !== undefined) {
return defaultValue
} else if (__DEV__) {
warn(`injection ${key} not found.`)
}
} }
} }