vue3的生命周期函数

在 Vue 3 的 Composition API 中,组件的生命周期钩子(如 onMountonBeforeMount 等)的调用顺序遵循组件的挂载和卸载流程。以下是它们的调用顺序,以及在父子组件中的表现:


单个组件的生命周期调用顺序

  1. onBeforeMount

    • 在组件挂载到 DOM 之前调用,此时模板已编译,但尚未渲染到 DOM。

  2. onMount

    • 在组件挂载到 DOM 之后调用,此时可以安全访问 DOM 元素。

  3. onBeforeUnmount

    • 在组件卸载(从 DOM 移除)之前调用,适合清理副作用(如定时器、事件监听)。

  4. onUnmounted

    • 在组件卸载之后调用,此时组件已从 DOM 中移除。


父子组件中的调用顺序

挂载阶段(父 → 子)
  1. 父组件的 onBeforeMount

  2. 子组件的 onBeforeMount

  3. 子组件的 onMount

  4. 父组件的 onMount

卸载阶段(父 → 子)
  1. 父组件的 onBeforeUnmount

  2. 子组件的 onBeforeUnmount

  3. 子组件的 onUnmounted

  4. 父组件的 onUnmounted


为什么是这样的顺序?

  • 挂载时:Vue 需要先确保父组件模板编译完成(父的 onBeforeMount),然后递归处理子组件的挂载(子的 onBeforeMountonMount),最后父组件才能完成挂载(父的 onMount)。

  • 卸载时:父组件触发卸载后,需要先清理子组件(保证资源释放),最后再清理自身。


示例代码

父组件
<script setup>
import { onBeforeMount, onMounted, onBeforeUnmount, onUnmounted } from 'vue';
import Child from './Child.vue';
​
onBeforeMount(() => console.log('父组件 - onBeforeMount'));
onMounted(() => console.log('父组件 - onMounted'));
onBeforeUnmount(() => console.log('父组件 - onBeforeUnmount'));
onUnmounted(() => console.log('父组件 - onUnmounted'));
</script>
​
<template>
  <Child />
</template>
子组件
<script setup>
import { onBeforeMount, onMounted, onBeforeUnmount, onUnmounted } from 'vue';
​
onBeforeMount(() => console.log('子组件 - onBeforeMount'));
onMounted(() => console.log('子组件 - onMounted'));
onBeforeUnmount(() => console.log('子组件 - onBeforeUnmount'));
onUnmounted(() => console.log('子组件 - onUnmounted'));
</script>
​
<template>Child Component</template>
控制台输出
父组件 - onBeforeMount
子组件 - onBeforeMount
子组件 - onMounted
父组件 - onMounted
​
父组件 - onBeforeUnmount
子组件 - onBeforeUnmount
子组件 - onUnmounted
父组件 - onUnmounted

关键点总结

  1. 生命周期顺序是同步的,遵循从外到内(挂载)和从内到外(卸载)的递归逻辑。

  2. 子组件的挂载必须在父组件挂载之前完成,而卸载时必须先卸载子组件。

  3. 如果需要操作 DOM,应在 onMounted 中执行;清理资源应在 onBeforeUnmountonUnmounted 中执行。

生命周期钩子调用时机典型用途
onBeforeMount挂载前访问数据但无法操作 DOM
onMounted挂载后操作 DOM、初始化第三方库
onBeforeUpdate更新前获取更新前的 DOM 状态
onUpdated更新后操作更新后的 DOM(慎用)
onBeforeUnmount卸载前清理定时器、事件监听
onUnmounted卸载后最终清理
onActivated<KeepAlive> 激活恢复组件状态
onDeactivated<KeepAlive> 停用暂停组件任务
onErrorCaptured捕获错误全局错误处理

Vue3生命周期函数是指在Vue3实例创建、更新、渲染销毁这几个重要阶段执行的函数。Vue3生命周期函数分为两类:组件生命周期函数全局生命周期函数。 首先是组件生命周期函数,包括beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeUnmountunmounted等方法。 beforeCreate:在实例初始化之后,数据观测事件配置之前被调用,此时 data methods 都还未被初始化。 created:组件实例已经完全创建,包括属性计算、watch/event 事件回调。在这里可以访问到已经存在的 DOM 元素,但是该组件的 DOM 元素尚未被渲染出来。 beforeMount:在挂载开始之前被调用,在此之前 template/render 函数已经完成编译。 mounted:组件挂载到 DOM 上后调用,此时真实的 DOM 元素已经生成,可以对 DOM 进行操作。 beforeUpdate:在数据更新之前被调用,此时可以进行修改数据操作。在此函数执行时组件 DOM 所依赖的 props computed 已经更新,但是尚未开始重新渲染 DOM。 updated:数据更新时调用,此时组件 DOM 已经重新渲染过,可以对 DOM 进行操作。 beforeUnmount:在卸载组件之前调用,此时组件实例仍然可以访问。 unmounted:组件卸载完成后调用,在这里执行一些清理工作,比如清除定时器、解除事件监听等。 接下来是全局生命周期函数,包括beforeCreate、created、beforeMount、mounted、beforeUpdate、updatederrorCaptured等方法。 errorCaptured:可以在组件的内部所抛出的异常被捕获处理后,再将这个异常最终传递给全局错误处理。在捕获到错误时,可以对错误进行处理,并使用 nextTick 在渲染更新后再将错误信息抛出。 总之,Vue3生命周期函数提供了方便的钩子函数来执行组件全局的自定义逻辑,允许开发人员在关键的阶段进行处理来实现更加复杂的业务逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值