1啤覆、
Vue2中的beforeDestroy 在Vue3中被 beforeUnmount取代
Vue2中的destroyed在Vue3中被 unmounted取代
2闪湾、
Vue2 -> Vue3
------------------------------------------
beforeCreate -> setup
created -> setup
beforeMount -> onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed -> onUnmounted
activated -> onActivated
deactivated -> onDeactivated
errorCaptured -> onErrorCaptured
//- onBeforeMount:DOM即將掛載
//- onMounted:DOM掛載完畢
//- onBeforeUpdate:DOM即將更新
//- onUpdated:DOM更新完畢
//- onBeforeUnmount:即將銷毀
//- onUnmounted:銷毀完畢
舉例:
import { defineComponent, onBeforeMount,onMounted } from 'vue'
export default defineComponent({
setup () {
onBeforeMount(() => {
console.log("DOM即將掛載");
})
onMounted(() => {
console.log("DOM掛載完畢");
})
//- onBeforeUpdate(() => {}) //- DOM即將更新
//- onUpdated(() => {}) //- DOM更新完畢
//- onBeforeUnmount(() => {}) //- 即將銷毀
//- onUnmounted(() => {}) //- 銷毀完畢
return {}
}
})