Vue 2 父子组件生命周期流程
beforeCreate() → created() → beforeMount()
beforeCreate() → created() → beforeMount() → mounted()
mounted()
父 beforeUpdate() → 子 beforeUpdate() → 子 updated() → 父 updated()
父 beforeDestroy() → 子 beforeDestroy() → 子 destroyed() → 父 destroyed()
Vue 3 父子组件生命周期流程
setup() → onBeforeMount()
setup() → onBeforeMount() → onMounted()
onMounted()
父 onBeforeUpdate() → 子 onBeforeUpdate() → 子 onUpdated() → 父 onUpdated()
父 onBeforeUnmount() → 子 onBeforeUnmount() → 子 onUnmounted() → 父 onUnmounted()
总结
- 顺序一致:Vue 2 和 Vue 3 的父子组件生命周期执行顺序逻辑相同。
- 差异在细节:Vue 3 通过组合式 API 提供更灵活的钩子注册方式,并调整了卸载阶段的钩子名称。
- 组合式 API:在 Vue 3 中,
setup() 替代了 beforeCreate 和 created,且组合式钩子优先于选项式钩子执行。