initExtend方法如何保证cachedCtors.SuperId获取到正确缓存
来源:3-2 createComponent

慕先生4328837
2022-06-09
每次执行initExtend, Vue.cid = 0, 即SuperId = 0, 每个组件optionsextendOptions._Ctor.0 = sub
, 都是0指向组件的构造函数。这样的话 为什么不直接用extendOptions._Ctor = sub
export function initExtend (Vue: GlobalAPI) {
Vue.cid = 0
let cid = 1
Vue.extend = function (extendOptions: Object): Function {
extendOptions = extendOptions || {}
const Super = this
const SuperId = Super.cid
const cachedCtors = extendOptions._Ctor || (extendOptions._Ctor = {})
if (cachedCtors[SuperId]) {
return cachedCtors[SuperId]
}
const Sub = function VueComponent (options) {
this._init(options)
}
cachedCtors[SuperId] = Sub
return Sub
}
}
写回答
1回答
-
/** * Each instance constructor, including Vue, has a unique * cid. This enables us to create wrapped "child * constructors" for prototypal inheritance and cache them. */ Vue.cid = 0 let cid = 1
其实源码的注释已经给答案了
042022-06-29
相似问题