apiInject.ts — vue Source File
Architecture documentation for apiInject.ts, a typescript file in the vue codebase. 3 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 83b69f04_ff47_67cf_7f1c_542b323e1eb5["apiInject.ts"] 9f3b1774_ebd5_0845_1a85_868e0c1fd480["currentInstance.ts"] 83b69f04_ff47_67cf_7f1c_542b323e1eb5 --> 9f3b1774_ebd5_0845_1a85_868e0c1fd480 6d8f8976_7066_720b_0d45_45fe42921eaf["util"] 83b69f04_ff47_67cf_7f1c_542b323e1eb5 --> 6d8f8976_7066_720b_0d45_45fe42921eaf 64c87498_c46a_6944_ab9d_8e45519852a8["component"] 83b69f04_ff47_67cf_7f1c_542b323e1eb5 --> 64c87498_c46a_6944_ab9d_8e45519852a8 style 83b69f04_ff47_67cf_7f1c_542b323e1eb5 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import { isFunction, warn } from 'core/util'
import { currentInstance } from './currentInstance'
import type { Component } from 'types/component'
export interface InjectionKey<T> extends Symbol {}
export function provide<T>(key: InjectionKey<T> | string | number, value: T) {
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.`)
}
}
Domain
Subdomains
Functions
Types
Dependencies
- component
- currentInstance.ts
- util
Source
Frequently Asked Questions
What does apiInject.ts do?
apiInject.ts is a source file in the vue codebase, written in typescript. It belongs to the VueCore domain, GlobalAPI subdomain.
What functions are defined in apiInject.ts?
apiInject.ts defines 3 function(s): inject, provide, resolveProvided.
What does apiInject.ts depend on?
apiInject.ts imports 3 module(s): component, currentInstance.ts, util.
Where is apiInject.ts in the architecture?
apiInject.ts is located at src/v3/apiInject.ts (domain: VueCore, subdomain: GlobalAPI, directory: src/v3).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free