exportfunctionprovide<T>(key: InjectionKey<T>|string|number,value: T){letprovides=currentInstance.provides// 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.
// 默认情况下,实例继承其父对象的提供对象
// 但是当它需要提供自己的价值时,它会创造自己的价值
// 自己提供对象使用父提供对象作为原型。
// 这样在 `inject` 中我们可以简单地从直接父级查找注入并让原型链完成工作。
constparentProvides=currentInstance.parent&¤tInstance.parent.providesif(parentProvides===provides){provides=currentInstance.provides=Object.create(parentProvides)}// TS doesn't allow symbol as index type
provides[keyasstring]=value}
exportfunctioninject(key: InjectionKey<any>|string,defaultValue?: unknown,treatDefaultAsFactory=false){// fallback to `currentRenderingInstance` so that this can be called in
// a functional component
constinstance=currentInstance||currentRenderingInstanceif(instance){// #2400
// to support `app.use` plugins,
// fallback to appContext's `provides` if the instance is at root
constprovides=instance.parent==null?instance.vnode.appContext&&instance.vnode.appContext.provides : instance.parent.providesif(provides&&(keyasstring|symbol)inprovides){// TS doesn't allow symbol as index type
returnprovides[keyasstring]}elseif(arguments.length>1){returntreatDefaultAsFactory&&isFunction(defaultValue)?defaultValue.call(instance.proxy):defaultValue}elseif(__DEV__){warn(`injection "${String(key)}" not found.`)}}elseif(__DEV__){warn(`inject() can only be used inside setup() or functional components.`)}}