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
exportfunctioncallHook(vm: Component,hook: string){// #7573 disable dep collection when invoking lifecycle hookspushTarget()consthandlers=vm.$options[hook]if(handlers){for(leti=0,j=handlers.length;i<j;i++){try{handlers[i].call(vm)}catch(e){handleError(e,vm,`${hook} hook`)}}}if(vm._hasHookEvent){vm.$emit('hook:'+hook)}popTarget()}
根据传入的字符串hook,去拿到 vm.$options[hook] 对应的回调函数数组,然后遍历执行,执行的时候把 vm 作为函数执行的上下文
beforeCreate & created
beforeCreate 和 created 函数都是在实例化 Vue 的阶段,在 _init 方法中执行的,它的定义在 src/core/instance/init.js 中
Vue.prototype._init=function(options?: Object){// ...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')// ...}
源码中最终执行生命周期的函数都是调用 callHook 方法,它的定义在
src/core/instance/lifecycle
中根据传入的字符串
hook
,去拿到vm.$options[hook]
对应的回调函数数组,然后遍历执行,执行的时候把 vm 作为函数执行的上下文beforeCreate & created
beforeCreate
和created
函数都是在实例化 Vue 的阶段,在_init
方法中执行的,它的定义在src/core/instance/init.js
中这两个生命钩子定义在
initState(vm)
前后initState(vm)
的作用是初始化props、data、methods、watch、computed
等属性所以
beforeCreate
钩子中是无法读取到上述的属性的同时这里也还没有渲染DOM,所以这两个生命钩子都不能访问DOM
如果想请求数据,这两个钩子都是OK的
不过如果要跟data、props里面的数据交互,就要在
created
中beforeMount & mounted
beforeMount
发生在DOM挂载之前,调用函数是mountComponent
,定义在src/core/instance/lifecycle.js
中beforeDestroy & destroyed
beforeDestroy 钩子函数的执行时机是在 $destroy 函数执行最开始的地方,接着执行了一系列的销毁动作,包括从 parent 的 $children 中删掉自身,删除 watcher,当前渲染的 VNode 执行销毁钩子函数等,执行完毕后再调用 destroy 钩子函数。
在 $destroy 的执行过程中,它又会执行 vm.patch(vm._vnode, null) 触发它子组件的销毁钩子函数,这样一层层的递归调用,所以 destroy 钩子函数执行顺序是先子后父,和 mounted 过程一样。
The text was updated successfully, but these errors were encountered: