MCPcopy
hub / github.com/vuejs/core / provide

Function provide

packages/runtime-core/src/apiInject.ts:10–34  ·  view source on GitHub ↗
(
  key: K,
  value: K extends InjectionKey<infer V> ? V : T,
)

Source from the content-addressed store, hash-verified

8export type InjectionKey<T> = symbol & InjectionConstraint<T>
9
10export function provide<T, K = InjectionKey<T> | string | number>(
11 key: K,
12 value: K extends InjectionKey<infer V> ? V : T,
13): void {
14 if (__DEV__) {
15 if (!currentInstance || currentInstance.isMounted) {
16 warn(`provide() can only be used inside setup().`)
17 }
18 }
19 if (currentInstance) {
20 let provides = currentInstance.provides
21 // by default an instance inherits its parent's provides object
22 // but when it needs to provide values of its own, it creates its
23 // own provides object using parent provides object as prototype.
24 // this way in `inject` we can simply look up injections from direct
25 // parent and let the prototype chain do the work.
26 const parentProvides =
27 currentInstance.parent && currentInstance.parent.provides
28 if (parentProvides === provides) {
29 provides = currentInstance.provides = Object.create(parentProvides)
30 }
31 // TS doesn't allow symbol as index type
32 provides[key as string] = value
33 }
34}
35
36export function inject<T>(key: InjectionKey<T> | string): T | undefined
37export function inject<T>(

Callers 2

setupFunction · 0.90
applyOptionsFunction · 0.90

Calls 1

warnFunction · 0.90

Tested by 1

setupFunction · 0.72