生命周期
# 生命周期
# Vue3 的生命周期
setup():开始创建组件之前,在beforeCreate和created之前执行。创建的是data和methodonBeforeMount(): 组件挂载到节点上之前执行的函数。onMounted(): 组件挂载完成后执行的函数。onBeforeUpdate(): 组件更新之前执行的函数。onUpdated(): 组件更新完成之后执行的函数。onBeforeUnmount(): 组件卸载之前执行的函数。onUnmounted(): 组件卸载完成后执行的函数onActivated(): 被包含在中的组件,会多出两个生命周期钩子函数。被激活时执行。onDeactivated(): 比如从 A 组件,切换到 B 组件,A 组件消失时执行。onErrorCaptured(): 当捕获一个来自子孙组件的异常时激活钩子函数(以后用到再讲,不好展现)。
# vue2 和vue3 生命周期对比
Vue3.0中可以继续使用Vue2.x中的生命周期钩子,但有有两个被更名:
beforeDestroy改名为beforeUnmountdestroyed改名为unmountedVue3.0也提供了 Composition API 形式的生命周期钩子,与Vue2.x中钩子对应关系如下:beforeCreate===>setup()created=======>setup()beforeMount===>onBeforeMountmounted=======>onMountedbeforeUpdate===>onBeforeUpdateupdated=======>onUpdatedbeforeUnmount==>onBeforeUnmountunmounted=====>onUnmounted
上次更新: 2025/06/23, 07:26:12