今天给大家分享Vue初始化中的选项合并之initInternalComponent的相关知识,具体代码如下所示:
export function initInternalComponent (vm: Component, options: InternalComponentOptions) { const opts = vm.$options = Object.create(vm.constructor.options) // doing this because it's faster than dynamic enumeration. const parentVnode = options._parentVnode opts.parent = options.parent opts._parentVnode = parentVnode const vnodeComponentOptions = parentVnode.componentOptions opts.propsData = vnodeComponentOptions.propsData opts._parentListeners = vnodeComponentOptions.listeners opts._renderChildren = vnodeComponentOptions.children opts._componentTag = vnodeComponentOptions.tag if (options.render) { opts.render = options.render opts.staticRenderFns = options.staticRenderFns } }
initInternalComponent
方法接受两个参数,第一个参数是组件实例,即this。第二个参数是组件构造函数中传入的option,这个option根据上文的分析,他是在createComponentInstanceForVnode
方法中定义的:
export function createComponentInstanceForVnode ( vnode: any, // we know it's MountedComponentVNode but flow doesn't parent: any, // activeInstance in lifecycle state ): Component { const options: InternalComponentOptions = { _isComponent: true, _parentVnode: vnode, parent } // check inline-template render functions const inlineTemplate = vnode.data.inlineTemplate if (isDef(inlineTemplate)) { options.render = inlineTemplate.render options.staticRenderFns = inlineTemplate.staticRenderFns } return new vnode.componentOptions.Ctor(options) }
option中有三个属性值,_isComponent
上面已经提到过了;_parentVode
其实就是该组件实例的vnode对象(createComponentInstanceForVnode
就是根据这个vnode对象去创建一个组件实例);parent
则是该组件的父组件实例对象。
然后我们来看看具体initInternalComponent
做了什么操作:
const opts = vm.$options = Object.create(vm.constructor.options)
首先,用Object.create
这个函数,把组件构造函数的options
挂载到vm.$options
的__proto__
上。
const parentVnode = options._parentVnode opts.parent = options.parent opts._parentVnode = parentVnode
接下把传入参数的option的_parentVode
和parent
挂载到组件实例$options
上。用我们在两种策略里的那个例子来说,parent
就是我们组件的根实例,而_parentVnode
就是<comp :msg="msg" @log-msg="logMsg"></comp>
生成的一个Vnode对象。
const vnodeComponentOptions = parentVnode.componentOptions opts.propsData = vnodeComponentOptions.propsData opts._parentListeners = vnodeComponentOptions.listeners opts._renderChildren = vnodeComponentOptions.children opts._componentTag = vnodeComponentOptions.tag
然后把父组件里的vnode上的四个属性挂载到我们的$options
上,还是用那个例子来说,propsData
就是根据:msg="msg"
生成的,他的值就是在根组件里定义的那个msg{msg: "props-message"}
。而_parentListeners
就是根据@log-msg="logMsg"
生成的,他的值是logMsg
这个定义在父组件中的方法。
if (options.render) { opts.render = options.render opts.staticRenderFns = options.staticRenderFns }
最后就是如果传入的option中如果有render,把render相关的也挂载到$options上。
因此,这个initInternalComponent
主要做了两件事情:1.指定组件$options原型,2.把组件依赖于父组件的props、listeners也挂载到options上,方便子组件调用。
总结
更新动态
- 原神:技术又升级啦?新增角色动作与神态,期待全方面普及
- 林俊杰.2020-幸存者(EP)【华纳】【WAV+CUE】
- 方志耀.1992-给我一点你的爱【飞碟】【WAV+CUE】
- 华晨宇.2015-异类【天娱传媒】【WAV+CUE】
- 月爱解说 王者荣耀一套带走爆发小狐狸妲己攻略
- 吴宇深《时光》[320K/MP3][61.94MB]
- 吴宇深《时光》[FLAC/分轨][86.52MB]
- 张宇《好男人的情歌 精选16首最爱发烧情歌》[WAV+CUE][600MB]
- 原神虹色大巡回第一关攻略 音符补缺打法位置分享
- 王者荣耀五五朋友星活动玩法介绍 规则一览
- 少年西游记2今日全平台公测 真国潮真轻松真福利
- 崩坏星穹铁道2.2光锥怎么抽 崩铁2.2光锥抽取建议
- 崩坏星穹铁道2.2无边曼舞光锥怎么样 崩铁新四星光锥无边曼舞强度
- 崩坏星穹铁道2.2夜色流光溢彩强度介绍 夜色流光溢彩光锥介绍
- 苹果安装双系统好不好,苹果装双系统好用吗