全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-20 7 分钟 ✍️ juanwangdev

生命周期概述

Vue 组件实例在创建过程中经历一系列初始化步骤。

生命周期阶段

JavaScript
创建阶段
├── setup() - Composition API 入口
├── beforeCreate - 实例初始化后
├── created - 实例创建完成
├── beforeMount - 挂载前
└── mounted - 挂载完成

更新阶段
├── beforeUpdate - 数据变化,DOM更新前
└── updated - DOM更新完成

卸载阶段
├── beforeUnmount - 卸载前
└── unmounted - 卸载完成

Composition API 钩子

JavaScript
import {
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted
} from 'vue'

export default {
  setup() {
    onMounted(() => console.log('mounted'))
    onUnmounted(() => console.log('unmounted'))
  }
}

Options API 钩子

text
export default {
  mounted() {
    console.log('mounted')
  },
  unmounted() {
    console.log('unmounted')
  }
}

生命周期流程图

text
setup/beforeCreate → created → beforeMount → mounted → beforeUpdate → updated → beforeUnmount → unmounted

Vue 3 中 beforeDestroydestroyed 更名为 beforeUnmountunmounted

要点总结

  • 生命周期分为创建、更新、卸载三个阶段
  • Composition API 使用 onXXX 函数
  • Options API 使用同名方法选项
  • Vue 3 更新了销毁钩子的命名

📝 发现内容有误?点击此处直接编辑

← 上一篇 父子组件生命周期顺序
下一篇 → Provide与Inject
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库