Vue 是组件化编程,从一个组件诞生到消亡会经历很多过程,这些过程就叫做生命周期。而伴随着生命周期,给用户使用的函数,就叫钩子函数,实际为操控生命周期。
本文主要对比 Vue2 生命周期,引出并展开 Vue3 生命周期的介绍,最后做一下整体的总结。
如果您仅需要速览,请查阅如下表格:
| Vue2.X (选项式 API) | Vue3.X (组合式 API) |
|---|---|
| setup() | |
| setup() | |
| beforeMount | onBeforeMount |
| mounted | onMounted |
| beforeUpdate | onBeforeUpdate |
| updated | onUpdated |
| beforeUnmount | onBeforeUnmount |
| unmounted | onUnmounted |
| errorCaptured | onErrorCaptured |
| renderTracked | onRenderTracked |
| renderTriggered | onRenderTriggered |
| activated | onActivated |