vue基础(五)

Vue 实例在创建、挂载、更新、销毁的过程中会触发一系列的生命周期钩子(Lifecycle Hooks),让开发者可以在不同阶段执行逻辑。

1. Vue 2 生命周期完整流程

生命周期的四个主要阶段

  1. 创建阶段(Creation)
  2. 挂载阶段(Mounting)
  3. 更新阶段(Updating)
  4. 销毁阶段(Destruction)

2. Vue 2 生命周期钩子(Hooks)

阶段 生命周期钩子 作用
创建前 beforeCreate 组件刚创建,还没有 datamethodscomputed
创建后 created datamethods 已初始化,但 DOM 还未渲染
挂载前 beforeMount template 解析完毕,还未挂载到真实 DOM
挂载后 mounted 组件挂载到 DOM,可以操作 DOM
更新前 beforeUpdate data 变化,DOM 还未更新
更新后 updated data 变化,DOM 已更新
销毁前 beforeDestroy 组件即将销毁,可清理定时器、解绑事件
销毁后 destroyed 组件已销毁,所有子组件也被销毁

3. Vue 2 生命周期示例

html 复制代码
<template>
  <div>
    <h1>{{ message }}</h1>
    <button @click="changeMessage">修改消息</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: "Hello Vue!"
    };
  },
  methods: {
    changeMessage() {
      this.message = "Vue 生命周期演示";
    }
  },
  beforeCreate() {
    console.log("1. beforeCreate - data 还未初始化", this.message); // undefined
  },
  created() {
    console.log("2. created - data 初始化完成", this.message);
  },
  beforeMount() {
    console.log("3. beforeMount - 模板编译完成,尚未挂载");
  },
  mounted() {
    console.log("4. mounted - 组件已挂载到 DOM,可进行 DOM 操作");
  },
  beforeUpdate() {
    console.log("5. beforeUpdate - data 变化,DOM 还未更新", this.message);
  },
  updated() {
    console.log("6. updated - data 变化,DOM 已更新", this.message);
  },
  beforeDestroy() {
    console.log("7. beforeDestroy - 组件即将销毁,可清理定时器等");
  },
  destroyed() {
    console.log("8. destroyed - 组件已销毁");
  }
};
</script>

4. Vue 3 生命周期

Vue 3 依然有生命周期钩子,但使用 setup() 时需要使用 Vue 3 提供的 onXxx 形式的钩子(从 vue 引入)。

Vue 3 生命周期对比

Vue 2 Vue 3 (Composition API)
beforeCreate setup() 直接初始化
created setup() 直接初始化
beforeMount onBeforeMount
mounted onMounted
beforeUpdate onBeforeUpdate
updated onUpdated
beforeDestroy onBeforeUnmount
destroyed onUnmounted

Vue 3 生命周期示例

html 复制代码
<template>
  <div>
    <h1>{{ message }}</h1>
    <button @click="changeMessage">修改消息</button>
  </div>
</template>

<script>
import { ref, onMounted, onBeforeUnmount, onUpdated } from "vue";

export default {
  setup() {
    const message = ref("Hello Vue 3!");

    const changeMessage = () => {
      message.value = "Vue 3 生命周期演示";
    };

    onMounted(() => {
      console.log("组件已挂载");
    });

    onUpdated(() => {
      console.log("组件更新了");
    });

    onBeforeUnmount(() => {
      console.log("组件即将销毁");
    });

    return { message, changeMessage };
  }
};
</script>

5. 生命周期使用场景

钩子 适用场景
beforeCreate 组件初始化前,可用于 console.log 调试
created 获取 data,初始化 Vuex,发起 Ajax 请求
beforeMount DOM 渲染前执行一些逻辑
mounted 获取 DOM,初始化 第三方库(如 ECharts
beforeUpdate data 变化时,执行一些更新前的计算
updated DOM 更新后执行操作(如日志记录)
beforeDestroy 清除 setInterval、解绑 window 事件
destroyed 组件销毁后的清理操作

6. Vue 生命周期面试题

Q1: createdmounted 有什么区别?

钩子 执行时机 适用场景
created 组件实例创建完成,但 DOM 还未渲染 可用于获取 data、调用 Vuex
mounted 组件挂载到 DOM 适用于 DOM 操作,如 ECharts

📌 如果要操作 DOM,需要在 mounted 中进行。

Q2: beforeDestroy 用来做什么?

beforeDestroy 适用于:

  • 清除定时器
  • 解绑全局事件
  • 销毁第三方库
html 复制代码
beforeDestroy() {
  clearInterval(this.timer); // 清除定时器
  window.removeEventListener("resize", this.handleResize); // 解绑事件
}
相关推荐
穷鱼子酱2 分钟前
ElSelect二次封装组件-实现分页(下拉加载、缓存)、回显
前端
科科睡不着3 分钟前
拆解iOS实况照片📷 - 附React web实现
前端
前端老兵AI3 分钟前
Electron 桌面应用开发入门:前端工程师的跨平台利器
前端·electron
胖子不胖5 分钟前
浅析cubic-bezier
前端
reasonsummer9 分钟前
【办公类-133-02】20260319_学区化展示PPT_02_python(图片合并文件夹、提取同名图片归类文件夹、图片编号、图片GIF)
前端·数据库·powerpoint
胡耀超25 分钟前
Web Crawling 网络爬虫全景:技术体系、反爬对抗与全链路成本分析
前端·爬虫·python·网络爬虫·数据采集·逆向工程·反爬虫
阿明的小蝴蝶29 分钟前
记一次Gradle环境的编译问题与解决
android·前端·gradle
Ruihong30 分钟前
【VuReact】轻松实现 Vue 到 React 路由适配
前端·react.js
山_雨31 分钟前
startViewTransition
前端
写代码的【黑咖啡】35 分钟前
Python Web 开发新宠:FastAPI 全面指南
前端·python·fastapi