美文网首页
Vue生命周期及其用法(2019-03-22)

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

作者: 你滴止痛药儿 | 来源:发表于2019-03-22 15:55 被阅读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

相关文章

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

    Vue框架生命周期总结 一、生命周期运行图 二、所有生命周期 三、用法 1.组件创建前 这个阶段主要是完成vue中...

  • Vue-计算属性和监听属性

    thiking 熟记Vue生命周期内的各个钩子函数以及其相应的对比和用法 计算属性(钩子函数--computed)...

  • Vue2.0生命周期钩子

    给大家分享一下我对Vue2.0的生命周期钩子的理解与用法 一、生命周期图示 二、生命周期分解 1、beforeCr...

  • 八、vue.js生命周期

    一、Vue生命周期 二、 计算属性 1.基本用法 2.计算属性 vs 方法 3.get和set 三、vue实例的属...

  • vue生命周期

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

  • vue生命周期

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

  • vue

    vue初级用法,数据绑定,以及页面渲染 初识vue 得先明白生命周期,才能更好的玩转他,就拿官网的图片吧,多看几次...

  • Vue生命周期

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

  • 如何在BroadcastReceiver中执行耗时操作

    在回答这个问题之前,首先简单介绍一下 BroadcastReceiver的用法应注意的地方及其生命周期。 一、Br...

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

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

网友评论

      本文标题:Vue生命周期及其用法(2019-03-22)

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