Vue生命周期及其用法(2019-03-22)

2019-03-22  本文已影响0人  你滴止痛药儿

Vue框架生命周期总结

一、生命周期运行图
3504099265-580628fd03258_articlex.png
二、所有生命周期
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
</head>
<body>

<div id="app">
     <p>{{ message }}</p>
</div>

<script type="text/javascript">
    
  var app = new Vue({
      el: '#app',
      data: {
          message : "xuxiao is boy" 
      },
       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); //已被初始化 
        },
        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); 
        },
        activated: function () {
            console.group('keep-alive 组件激活时调用===============》');
            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); 
        },
        deactivated: function () {
            console.group('keep-alive 组件停用时调用===============》');
            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>
</body>
</html>
三、用法
1.组件创建前
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)  
},

这个阶段主要是完成vue中关于生成周期以及事件的一些初始化工作,在这之前它会执行一个mergeOptions函数,得到$options选项,并把它设置成Vue实例的一个属性。

如何在这个生命周期中拿到data
1.setTimeout 相当于在初始化前告诉容器,等全执行完了再跑里面的代码。这种方式别说拿data了,拿渲染完DOM都OK~
2.直接从this.$ptions.data里去拿, 在beforeCreate前,所有的options都会先存到vm. options中,在beforeCreate之后,将options里的data啦,props啦,methods啦等等一个个附到vm上。

2.组件创建完毕
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); //已被初始化
},

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

一般在这个生命周期调用ajax访问后台接口获取页面初始化所需的数据。

3.组件挂载前
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); //已被初始化  
},

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

4.组件挂载前
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); //已被初始化 
},

el 被新创建的 vm.$el 替换,并挂载到实例上去之后调用该钩子。

在模板渲染成html后调用,通常是初始化页面完成后,再对html的dom节点进行一些需要的操作。

5.组件更新前
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); 
},

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

6.组件更新后
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); 
},

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

当这个钩子被调用时,组件 DOM 已经更新,所以你现在可以执行依赖于 DOM 的操作。然而在大多数情况下,你应该避免在此期间更改状态。如果要相应状态改变,通常最好使用计算属性watcher 取而代之。

7.组件销毁之前
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); 
},

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

8.组件销毁完成
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)
}

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

有些业务是离开页面访问后台接口改变数据的,可以放在销毁两个生命周期中。

完T_T

上一篇下一篇

猜你喜欢

热点阅读