10分钟看懂vue生命周期

本文详细解析了Vue.js的生命周期,从创建、初始化、挂载、更新到销毁的全过程,包括beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy和destroyed等阶段的状态和作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是生命周期?

简而言之:从生到死的过程,从Vue实例创建-运行-销毁的过程
Vue实例有一个完整的生命周期,也就是从开始创建、初始化数据、编译模板、挂载Dom、渲染→更新→渲染、销毁等一系列过程

  • 创建期间生命周期方法
    beforeCreate:
    created:
    beforeMount
    mounted
  • 运行期间生命周期方法
    beforeUpdate
    updated
  • 销毁期间的生命周期方法
    beforeDestroy
    destroyed
图示

vue生命周期图

代码
<template>
  <div>
    <div id="msg">{{msg}}</div>
  </div>
</template>

<script>
  export default {
    name: 'vue生命周期',
    data () {
      return {
        msg: '我是谁?我在那?'
      }
    },
    methods: {
      myMethods (val) {
        console.log("我是methods,我什么时候出来?答案是===" + val);
      }
    },
    beforeCreate () {
      // 执行beforeCreate的时候,表示Vue刚刚出生,还没有任何内容,el/data/methods都没有初始化
      // 执行方法会报错
      console.group('beforeCreate 创建前状态===============》');
      console.log("%c%s", "color:red" , "el  :" + this.$el); //undefined
      console.log("%c%s", "color:red",  "data:" + this.$data); //undefined
      console.log("%c%s", "color:red" , "msg :" +  this.msg) // undefined
     // this.myMethods('beforeCreate') // Error in beforeCreate hook: "TypeError: this.myMethods is not a function"
    },
    created () {
      // 执行created的时候,表示Vue实例已经初始化好了部分内容,el/data/methods
      // 想在Vue实例中最早访问到data/methods,只有在这个方法才能访问
      console.group('created 创建完毕状态===============》');
      console.log("%c%s", "color:red","el  :" + this.$el); //undefined
      console.log("%c%s", "color:red","data:" + this.$data); //已被初始化
      console.log("%c%s", "color:red","msg :" + this.msg); //已被初始化
      this.myMethods('created') //已被初始化
    },
    beforeMount () {
      // 执行beforeMount,表示已经根据数据编译好了模板,但是还没有渲染到界面上
      console.group('beforeMount 挂载前状态===============》');
      console.log("%c%s", "color:red","el  :" + this.$el); //undefined
      console.log("%c%s", "color:red","data:" + this.$data); //已被初始化
      console.log("%c%s", "color:red","msg :" + this.msg); //已被初始化
    },
    mounted () {
      // 执行mounted,表示已经根据数据编译好了模板,已经将模板有渲染到界面上,此时可以对界面进行其他操作了
      console.group('mounted 挂载结束状态===============》');
      console.log("%c%s", "color:red","el  :" + this.$el.innerHTML); //已被初始化
      console.log(this.$el);
      console.log("%c%s", "color:red","data:" + this.$data); //已被初始化
      console.log("%c%s", "color:red","msg :" + this.msg); //已被初始化
    },
    beforeUpdate () {
      // 主要data中的数据发生了变化就会执行
      // 执行beforeUpdate时候,data的数据已经是最新的了,但是没有更新界面上的数据
      console.group('beforeUpdate 更新前状态===============》');
      console.log("%c%s", "color:red","el  :" + this.$el.innerHTML);
      console.log(this.$el);
      console.log("%c%s", "color:red","data:" + this.$data);
      console.log("%c%s", "color:red","msg :" + this.msg);
    },
    updated () {
      // 主要data中的数据发生了变化就会执行
      // 执行updated时候,data的数据已经是最新的了,界面上的数据也已经更新
      console.group('updated 更新完成状态===============》');
      console.log("%c%s", "color:red","el  :" + this.$el);
      console.log(this.$el);
      console.log("%c%s", "color:red","data:" + this.$data);
      console.log("%c%s", "color:red","msg :" + this.msg);
    },
    beforeDestroy () {
      // 执行beforeDestroy的时候,表示Vue实例即将销毁,但是还未销毁,实例中的数据等都可以使用,
      // 最后能使用Vue实例的地址
      console.group('beforeDestroy 销毁前状态===============》');
      console.log("%c%s", "color:red","el  :" + this.$el);
      console.log(this.$el);
      console.log("%c%s", "color:red","data:" + this.$data);
      console.log("%c%s", "color:red","msg :" + this.msg);
    },
    destroyed () {
      // 执行destroyed的时候,表示vue实例完全销毁,实例中的任何内容都不能被使用了
      console.group('destroyed 销毁完成状态===============》');
      console.log("%c%s", "color:red","el  :" + this.$el);
      console.log(this.$el);
      console.log("%c%s", "color:red","data:" + this.$data);
      console.log("%c%s", "color:red","msg :" + this.msg)
    }

  }
</script>
控制台信息

控制台信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值