Vue3和Vue2生命周期对比
- 更名
- beforeDestroy改名为beforeUnmount
- destroyed改名为unmounted
- Vue3中提供了Composition API形式的生命周期狗子,与Vue2中的狗子对应关系如下:
- beforeCreate — setup()
- created — setup()
- beforeMount — onBeforeMount
- mounted — onMounted
- beforeUpdate — onBeforeUpdate
- updated — onUpdated
- beforeUnmount — onBeforeUnmount
- unmounted — onUnmounted
import {
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted
} from 'vue'
// 通过组合式API的形式去使用生命周期钩子
onBeforeMount(() => {
})
onMounted(() => {
})
onBeforeUpdate(() => {
})
onUpdated(() => {
})
onBeforeUnmount(() => {
})
onUnmounted(() => {
})