1,既可以用组合式API的方式,也可以用vue2那种方式,不过是在setup方法外面。前者先执行。
2,注意通过组合式API的方式,把生命周期函数当作配置项的使用时一定要先导入该函数。
3,setup函数内部写法:
-
Vue3.0中可以继续使用Vue2.x中的生命周期钩子,但有有两个被更名:
beforeDestroy改名为 beforeUnmount destroyed改名为 unmounted
-
Vue3.0也提供了 Composition API 形式的生命周期钩子,与Vue2.x中钩子对应关系如下:
beforeCreate===>setup() created=======>setup() beforeMount ===>onBeforeMount mounted=======>onMounted beforeUpdate===>onBeforeUpdate updated =======>onUpdated beforeUnmount ==>onBeforeUnmount unmounted =====>onUnmounted