Vue 2.0 浅谈--生命周期和钩子函数

前言

用Vue也有一段时间了,发现生命周期是很重要的一部分,稍微懂得了一些东西,特意来分享一下.

生命周期和钩子函数-介绍

啥也不说先上图
图-1为 Vue 1.0 生命周期图,图-2为 Vue 2.0 生命周期图,图-3为Vue 1.0 和 Vue 2.0 钩子函数比较
重点看 Vue 2.0javascript

图片描述

图片描述

图片描述

生命周期和钩子函数-具体

上代码html

本身粘走执行
<!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 : "Hello 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); //已被初始化 
        },
        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>
</body>
</html>

打开F12能够查看生命周期各个时期的钩子函数的状态,以下图vue

图片描述

由上图知:java

1.beforeCrete: 此时,$el和data都为undefined,没有初始化
2.created: 建立后data初始化了,而$el没有
3.brforeMount: 挂在以前,$el和data都初始化了
4.mounted: Vue实例挂载完成了
注意: beforeMount红色矩形框里是{{message}},mounted的红矩形框里是xuxiao is boy,说明挂载前$el的值为'虚拟'的元素节点,挂载后'虚拟'的Dom节点被真实的Dom节点替换

数据更新(update)

在控制台里输入app.message = '数据更新'后

以下图
图片描述segmentfault

因而可知,当data数据变化时只会触发update

Vue实例解耦(destroy)

在控制台里输入app.$destroy();
以下图
图片描述

由图知:后端

执行完destroy操做后,data里的数据没有变化,可是Dom结构还存在,也就是Vue实例再也不受控制了,完成了解耦

生命周期和钩子函数-总结

以下图
这是把官方 Vue 2.0 生命周期的图例简化后的app

图片描述

生命周期钩子函数使用

beforecreate : 举个栗子:能够在这加个loading事件
created :在这结束loading,还作一些初始化,实现函数自执行
mounted : 在这发起后端请求,拿回数据,配合路由钩子作一些事情
beforeDestory: 你确认删除XX吗? destoryed :当前组件已被删除,清空相关内容

最后的寄语

第一次在segmentfault写东西,写的不对的地方请多多见谅,也请帮我指正出来!函数

参考文章

https://segmentfault.com/a/11...
http://www.cnblogs.com/gagag/...
相关文章
相关标签/搜索