vue3provide和reject
//APP.vue ⽗组件传递值
// tup(){
// let num = ref(1)
// provide('num',num)
// }
//⼦组件接收值
// tup(){
// var result = inject('num')
练字的10大好处// console.log(result)
// }
let currentInstance = null;
function provide(key, value) {
if (!currentInstance) {
{
warn$1(`provide() can only be ud inside tup().`);
}
}
el {
let provides = 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.
//默认情况下,实例继承其⽗对象
//但当它需要提供⾃⼰的价值时,它就会创造⾃⼰的价值
//own使⽤⽗对象作为原型提供对象。
儿童健康零食
//通过这种⽅式,在'inject'中,我们可以简单地从direct中查找注⼊
//创建⽗对象并让原型链完成⼯作。
const parentProvides = currentInstance.parent && currentInstance.parent.provides;
if (parentProvides === provides) {
provides = currentInstance.provides = ate(parentProvides);
}
// TS doesn't allow symbol as index type
//TS不允许将符号作为索引类型
provides[key] = value;
}
}
function inject(key, defaultValue, treatDefaultAsFactory = fal) {
// fallback to `currentRenderingInstance` so that this can be called in
// a functional component
//回退到'currentRenderingInstance',以便可以在中调⽤它
//功能部件
const instance = currentInstance || currentRenderingInstance;
赐予
if (instance) {
// #2400
关于读书的议论文
// to support `app.u` plugins,
// fallback to appContext's `provides` if the intance is at root
//要⽀持'app.u'插件,
//如果intance位于根⽬录下,则回退到appContext的'provides'
const provides = instance.parent == null
instance.vnode.appContext && instance.vnode.appContext.provides
: instance.parent.provides;
鳍
if (provides && key in provides) {
/
/ TS doesn't allow symbol as index type
return provides[key];
}
el if (arguments.length > 1) {
return treatDefaultAsFactory && isFunction(defaultValue)
return treatDefaultAsFactory && isFunction(defaultValue)
defaultValue.call(instance.proxy)
: defaultValue;
}
2018春节联欢晚会
el {
warn$1(`injection "${String(key)}" not found.`);
中信部
}
}
el {
warn$1(`inject() can only be ud inside tup() or functional components.`); }
}