美文网首页
四、Vue生命周期

四、Vue生命周期

作者: Epat | 来源:发表于2019-05-31 16:32 被阅读0次

一、生命周期图解

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(2)生命周期

    一、Vue生命周期 vue 生命周期有 创建、挂载、更新、销毁 四个阶段 创建前(beforeCreate):实例...

  • vue生命周期

    一 vue生命周期 Vue的生命周期:就是vue实例从创建到销毁的全过程 二 vue生命周期钩子 vue生命周期...

  • vue生命周期

    vue生命周期详: vue生命周期详解图: vue生命周期详解代码展示:

  • Vue生命周期

    Vue生命周期详解 一、Vue生命周期 与 作用 (生命周期钩子就是生命周期函数) (1)Vue生命周期:每个Vu...

  • 前端面试题-面试用

    一、Vue二、JS三、Html + CSS四、网络相关 一、vue 一)、 生命周期 beforeCreate(创...

  • vue3较vue2的特别之处 - 生命周期

    vue2 生命周期 vue3 生命周期 将 Vue2 的生命周期钩子代码更新到 Vue3 参考地址:https:/...

  • 前端之路-VUE面试题

    vue生命周期面试题vue 生命周期是什么? Vue 实例从创建到销毁的过程,就是生命周期 beforeCreat...

  • Vue.js入门(二):生命周期

    1 生命周期 Vue生命周期是Vue实例从创建到销毁的过程,就是生命周期。在Vue实例的生命周期过程中会运行生命周...

  • vue生命周期

    vue里的生命周期是什么? vue实例从创建到销毁的过程称之为vue的生命周期 vue的生命周期各阶段都做了什么?...

  • Vue 生命周期

    vue里的生命周期是什么? vue实例从创建到销毁的过程称之为vue的生命周期 vue的生命周期各阶段都做了什么?...

网友评论

      本文标题:四、Vue生命周期

      本文链接:https://www.haomeiwen.com/subject/ajqvtctx.html