The document describes functions for dependency injection in Vue components. It defines functions for providing dependencies to child components using keys (provide), resolving provided dependencies for a component (resolveProvided), and injecting dependencies into a component (inject). Inject looks for keys in the parent's provided dependencies first before falling back to default values or throwing warnings.
The document describes functions for dependency injection in Vue components. It defines functions for providing dependencies to child components using keys (provide), resolving provided dependencies for a component (resolveProvided), and injecting dependencies into a component (inject). Inject looks for keys in the parent's provided dependencies first before falling back to default values or throwing warnings.
The document describes functions for dependency injection in Vue components. It defines functions for providing dependencies to child components using keys (provide), resolving provided dependencies for a component (resolveProvided), and injecting dependencies into a component (inject). Inject looks for keys in the parent's provided dependencies first before falling back to default values or throwing warnings.
The document describes functions for dependency injection in Vue components. It defines functions for providing dependencies to child components using keys (provide), resolving provided dependencies for a component (resolveProvided), and injecting dependencies into a component (inject). Inject looks for keys in the parent's provided dependencies first before falling back to default values or throwing warnings.
if (!currentInstance) { if (__DEV__) { warn(`provide() can only be used inside setup().`) } } else { // TS doesn't allow symbol as index type resolveProvided(currentInstance)[key as string] = value } }
export function resolveProvided(vm: Component): Record<string, any> {
// by default an instance inherits its parent's provides object // but when it needs to provide values of its own, it creates its // own provides object using parent provides object as prototype. // this way in `inject` we can simply look up injections from direct // parent and let the prototype chain do the work. const existing = vm._provided const parentProvides = vm.$parent && vm.$parent._provided if (parentProvides === existing) { return (vm._provided = Object.create(parentProvides)) } else { return existing } }
export function inject<T>(key: InjectionKey<T> | string): T | undefined
export function inject<T>( key: InjectionKey<T> | string, defaultValue: T, treatDefaultAsFactory?: false ): T export function inject<T>( key: InjectionKey<T> | string, defaultValue: T | (() => T), treatDefaultAsFactory: true ): T export function inject( key: InjectionKey<any> | string, defaultValue?: unknown, treatDefaultAsFactory = false ) { // fallback to `currentRenderingInstance` so that this can be called in // a functional component const instance = currentInstance if (instance) { // #2400 // to support `app.use` plugins, // fallback to appContext's `provides` if the instance is at root const provides = instance.$parent && instance.$parent._provided
if (provides && (key as string | symbol) in provides) {
// TS doesn't allow symbol as index type return provides[key as string] } else if (arguments.length > 1) { return treatDefaultAsFactory && isFunction(defaultValue) ? defaultValue.call(instance) : defaultValue } else if (__DEV__) { warn(`injection "${String(key)}" not found.`) } } else if (__DEV__) { warn(`inject() can only be used inside setup() or functional components.`) } }