在 Vue 3 中,生命周期方法的名称有所改变。下面是 Vue 3 中的生命周期方法:
beforeCreate
->setup()
前,初始化props
、data
created
->setup()
内部,初始化后创建beforeMount
->onBeforeMount
组件挂载之前mounted
->onMounted
组件挂载之后beforeUpdate
->onBeforeUpdate
组件更新之前updated
->onUpdated
组件更新之后beforeUnmount
->onBeforeUnmount
组件卸载之前unmounted
->onUnmounted
组件卸载之后errorCaptured
->onErrorCaptured
捕获组件内部错误
除了名称变化外,Vue 3 也引入了新的生命周期 API,如:
onRenderTracked
在依赖项被追踪时调用onRenderTriggered
在组件重新渲染时调用
import { onMounted
} from 'vue'
onMounted
( () => {})