四、Vue生命周期

一、生命周期图解

Vue生命周期

正如生命有者生老病死一样,程序一样有和生命类似的周期,Vue为开发者定义了11个生命周期钩子,便于开发者在Vue处于不同状态时调用,他们分别是:

  • beforeCreate
  • created
  • beforeMount
  • mounted
  • beforeUpdate
  • updated
  • activated
  • deactivated
  • beforeDestroy
  • destroyed
  • errorCaptured (Vue 2.5 版本新增)
beforeCreate

在Vue实例初始化之后,数据观测 (data observer) 和 event/watcher 事件配置之前被调用

created

在实例创建完成后被立即调用。在这一步,实例已完成以下的配置:数据观测 (data observer),属性和方法的运算,watch/event 事件回调。然而,dom渲染、挂载阶段还没开始,$el 属性目前不可见

beforeMount

在挂载开始之前被调用:相关的 render 函数首次被调用

mount

dom渲染、挂载完成之后调用该钩子。

注意这里dom挂载完成不包括子组件

beforeUpdate

数据更新时调用,发生在 DOM 重新渲染、打补丁之前
这里适合在更新之前访问现有的 DOM,比如手动移除已添加的事件监听器

updated

由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子

activated

keep-alive 组件激活时调用

deactivated

keep-alive 组件停用时调用

keep-alive 具体参见Vue内置组件keep-alive

beforeDestroy

实例销毁之前调用。在这一步,实例仍然完全可用

destroyed

Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例都会被销毁

errorCaptured

当捕获一个来自子孙组件的错误时被调用。此钩子会收到三个参数:错误对象、发生错误的组件实例以及一个包含错误来源信息的字符串。此钩子可以返回 false 以阻止该错误继续向上传播
我们可以用如下代码来测试Vue基本的生命周期执行顺序

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>vue生命周期学习</title>
  <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
</head>
<body>
  <div id="app">
    <h1>{{message}}</h1>
  </div>
</body>
<script>
  var vm = new Vue({
    el: '#app',
    data: {
      message: 'Vue的生命周期'
    },
    beforeCreate: function() {
      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","message: " + this.message) 
    },
    created: function() {
      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","message: " + this.message); //已被初始化
    },
    beforeMount: function() {
      console.group('------beforeMount挂载前状态------');
      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","message: " + this.message); //已被初始化  
    },
    mounted: function() {
      console.group('------mounted 挂载结束状态------');
      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","message: " + this.message); //已被初始化
      this.message = 'mounted'
      setTimeout(function () {
        vm.$destroy()
      }, 200)
    },
    beforeUpdate: function () {
      console.group('beforeUpdate 更新前状态===============》');
      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","message: " + this.message);
    },
    updated: function () {
      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","message: " + this.message); 
    },
    beforeDestroy: function () {
      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","message: " + this.message); 
    },
    destroyed: function () {
      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","message: " + this.message)
    }
  })
</script>
</html>

在浏览器中打开控制台,就可以看到Vue整个生命周期执行顺序
可以看到在beforeCreate - created 后 $data 被挂载

生命周期

beforeMount - mounted 后 $el被挂载并更新


生命周期

在mounted 改变data中的message后 beforeUpdate - updated 被调用


生命周期

最后在mounted 200 毫秒后 手动调用 vm.$destroy(), 销毁Vue对象,beforeDestroy - destroyed 被调用


生命周期

Vue代码中使用的mounted函数为Vue独有的生命周期方法,要了解生命周期请参考
Vue生命周期文档
生命周期图解
生命周期测试代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • vue生命周期 每个Vue实例或组件从创建到显示再到废弃的过程就是vue的生命周期。很多时候我们希望能在这个过程中...
    皮皮坤666阅读 29,159评论 1 18
  • 1.vue基本生命周期 vue源码中最终执行生命周期函数都是调用callHook方法,callHook函数的逻辑很...
    WHU_GIS_LJ阅读 19,660评论 0 13
  • 参照翻译 Understanding Vue.js Lifecycle Hooks 理解组件的生命周期,有利于我们...
    bacbcc94613b阅读 5,093评论 0 6
  • 每个vue实例从创建到销毁的过程都是一个生命周期,也会运行对应的钩子函数,下图为Vue生命周期示意图: 1.bef...
    yun_154192阅读 12,682评论 1 7
  • 与密友闲聊间,忽然被问到,至今为止有没有一些做过一些后悔的决定啊?乍一被问,还真心的思虑了一下:生活了三十多...
    小饼子的记事本阅读 217评论 3 4