type
status
date
slug
summary
tags
category
icon
password
First
生命周期历程:
notion image
Then
解释:在vue中写入对应生命周期函数 首先会触发createHooks
notion image
这里的每个生命周期被vue定义了别名 其实就是枚举
notion image
在Vue 中getCurrentInstance()可以获取当前组件的实例 实例中会有m,c等的函数其实就是被挂载到实例的生命周期函数
再说createHooks 这个函数调用了injectHooks(这里使用了函数科里化 因为这些hook都是生命周期函数 所以只有第一个参数不同 其他参数通过柯里化省略)
看看injectHooks
notion image
如果有实例的话 把hooks初始化为对应的生命周期函数 如果没有生命周期函数 就初始化为空数组
notion image
以上就是对生命周期函数的初始化
 
之后生命周期函数就在对应的时间点进行执行
在renderer.ts → componentUpdateFn()
notion image
这里有个判断 走if即还没挂载 也就是组件初始化
notion image
先执行这个钩子 然后做一些render和patch 挂载vnode
现在有dom元素了 所以
notion image
组件初始化结束 然后就是else的更新
notion image
这时候dom还没更新 所以render+patch
然后
notion image
卸载在另一个函数 unmount()
首先清空收集的依赖和副作用函数
然后执行onBeforeMount
notion image
然后清空子树
notion image
最后执行unmounted
notion image
Vue Keep-Alive源码Vue3响应式原理