合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
>[success] # 生命周期 1. 与 2.x 版本生命周期相对应的组合式 API * beforeCreate -> 使用 setup() * created -> 使用 setup() * beforeMount -> onBeforeMount * mounted -> onMounted * beforeUpdate -> onBeforeUpdate * updated -> onUpdated * beforeDestroy -> onBeforeUnmount * destroyed -> onUnmounted * errorCaptured -> onErrorCaptured 2. 新增的钩子函数组合式 API 还提供了以下调试钩子函数: * onRenderTracked * onRenderTriggered 3.[`<KeepAlive>`](https://cn.vuejs.org/api/built-in-components.html#keepalive) 触发的钩子 `onActivated ` `onDeactivated` >[danger] ##### 案例 ~~~html <template> <div class="about"> <h2>msg: {{msg}}</h2> <hr> <button @click="update">更新</button> </div> </template> <script lang="ts"> import { ref, onMounted, onUpdated, onUnmounted, onBeforeMount, onBeforeUpdate, onBeforeUnmount } from "vue" export default { beforeCreate () { console.log('beforeCreate()') }, created () { console.log('created') }, beforeMount () { console.log('beforeMount') }, mounted () { console.log('mounted') }, beforeUpdate () { console.log('beforeUpdate') }, updated () { console.log('updated') }, beforeUnmount () { console.log('beforeUnmount') }, unmounted () { console.log('unmounted') }, setup() { console.log('setup') const msg = ref('abc') const update = () => { msg.value += '--' } onBeforeMount(() => { console.log('--onBeforeMount') }) onMounted(() => { console.log('--onMounted') }) onBeforeUpdate(() => { console.log('--onBeforeUpdate') }) onUpdated(() => { console.log('--onUpdated') }) onBeforeUnmount(() => { console.log('--onBeforeUnmount') }) onUnmounted(() => { console.log('--onUnmounted') }) return { msg, update } } } </script> ~~~ * 第一次执行 ![](https://img.kancloud.cn/4b/63/4b634087afb8fd220223c4e07c7fdd6a_165x143.png) * 点击更新按钮后执行 ![](https://img.kancloud.cn/04/0d/040d1e139ac2e8609e3b0db56e13b989_190x101.png) >[info] ## 官网 [生命周期钩子](https://cn.vuejs.org/api/composition-api-lifecycle.html#onactivated)