You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vue.prototype._init=function(options){//some code ...initLifecycle(vm);initEvents(vm);initRender(vm);callHook(vm,'beforeCreate');initInjections(vm);// resolve injections before data/propsinitState(vm);initProvide(vm);// resolve provide after data/propscallHook(vm,'created');//some code ...if(vm.$options.el){vm.$mount(vm.$options.el);}}
Watcher.prototype.get=functionget(){pushTarget(this);varvalue;varvm=this.vm;try{value=this.getter.call(vm,vm);}catch(e){if(this.user){handleError(e,vm,"getter for watcher \""+this.expression+"\"");}else{throwe;}}finally{// "touch" every property so they are all tracked as// dependencies for deep watchingif(this.deep){traverse(value);}popTarget();this.cleanupDeps();}returnvalue;};
init: functioninit(vnode,hydrating,parentElm,refElm){if(vnode.componentInstance&&!vnode.componentInstance._isDestroyed&&vnode.data.keepAlive){// kept-alive components, treat as a patchvarmountedNode=vnode;// work around flowcomponentVNodeHooks.prepatch(mountedNode,mountedNode);}else{varchild=vnode.componentInstance=createComponentInstanceForVnode(vnode,activeInstance,parentElm,refElm);//创建组件child.$mount(hydrating ? vnode.elm : undefined,hydrating);//开始挂载}}
functioncreateElm(vnode,insertedVnodeQueue,parentElm,refElm,nested,ownerArray,index){//some code ...vnode.isRootInsert=!nested;// for transition enter checkif(createComponent(vnode,insertedVnodeQueue,parentElm,refElm)){return;}//some code ...{createChildren(vnode,children,insertedVnodeQueue);if(isDef(data)){invokeCreateHooks(vnode,insertedVnodeQueue);}insert(parentElm,vnode.elm,refElm);}}
1 vm.$mount
在vue 进行init之后,将会进入挂载阶段,也就是在created生命周期之后。
2 mountComponent(vm, el, hydrating)
2.1 首先触发beforeMount周期钩子函数
2.2 生成updataComponent方法
2.3 创建一个Wather监听器
function Watcher(vm, expOrFn, cb, options, isRenderWatcher)
updateComponent 方法被赋值给Wather中的getter方法,且生成的Wather为renderWather,然后赋值给vm的wather属性,并push进wathers数组中,进行记录。
然后判定lazy属性,如果非lazy,则调用wather的get方法。
Watcher.prototype.get
1 当前watcher压入target数组
2 调用getter方法,也就是上面的mountComponent方法。
3 弹出target
4 清除Dep
2.4 回溯调用mounted周期钩子函数,注意这里会涉及到回溯。
3 updataComponent
调用_update方法进行更新。
4 Vue.prototype._render
5 _update
如果已经 mounted过了,则触发beforeUpdate周期。
否则开始进行__patch__.
6
__patch__
1 如果新旧节点都为vnode,则进行patchNode,这一部分用来用来对比vnode的差异。
2 如果新节点isRealElement(为元素节点),则直接生成一个空的vnode,并且根据元素信息调用createEle,生成新的节点。
7 createElm(vnode, insertedVnodeQueue, parentElm, refElm, nested, ownerArray, index)
在createEle中会进行创建Component.
8 createComponent(vnode, insertedVnodeQueue, parentElm, refElm)
这个方法就是根据vnode来创建组件,首先会检测一下vnode.data中有没有hook和init方法,
然后调用init方法,进行创建组建,并传入vnode.
9 componentVNodeHooks中的init(vnode, hydrating, parentElm, refElm)
10 createComponentInstanceForVnode(vnode, parent, parentElm, refElm)
根据根据vnode创建Component。
而这个 vnode.componentOptions.Ctor 就是Vue的构造方法,就是下面这个东西。
11 createChildren
然后进行创建子节点
重复调用createEle方法进行创建组件,不停的递归调用下去。
然后触发相应的钩子函数invokeCreateHooks,然后执行insert操作。
The text was updated successfully, but these errors were encountered: