最近的項目都使用vue2.0來開發,不得不說,vue真的非常好用,大大減少了項目的開發周期。在踩坑的過程中,因為對vue的生命周期不是特別了解,所以有時候會在幾個鈎子函數里做一些事情,什么時候做,在哪個函數里做,我們不清楚。
下面來總結一下vue的生命周期。
vue生命周期簡介


咱們從上圖可以很明顯的看出現在vue2.0都包括了哪些生命周期的函數了。
生命周期探究
對於執行順序和什么時候執行,看上面兩個圖基本有個了解了。下面我們將結合代碼去看看鈎子函數的執行。
<!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);
},
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>
點擊查看demo:http://js.jirengu.com/pikorecuzu
create 和 mounted 相關
咱們在chrome瀏覽器里打開,F12看console就能發現
beforecreated:el 和 data 並未初始化
created:完成了 data 數據的初始化,el沒有
beforeMount:完成了 el 和 data 初始化
mounted :完成掛載
另外在標紅處,我們能發現el還是 {{message}},這里就是應用的 Virtual DOM(虛擬Dom)技術,先把坑占住了。到后面mounted掛載的時候再把值渲染進去。

update 相關
這里我們在 chrome console里執行以下命令
app.message= 'yes !! I do';
下面就能看到data里的值被修改后,將會觸發update的操作。

destroy 相關
有關於銷毀,暫時還不是很清楚。我們在console里執行下命令對 vue實例進行銷毀。銷毀完成后,我們再重新改變message的值,vue不再對此動作進行響應了。但是原先生成的dom元素還存在,可以這么理解,執行了destroy操作,后續就不再受vue控制了。
app.$destroy();

生命周期總結
beforecreate : 舉個栗子:可以在這加個loading事件
created :在這結束loading,還做一些初始化,實現函數自執行
mounted : 在這發起后端請求,拿回數據,配合路由鈎子做一些事情
beforeDestory: 你確認刪除XX嗎? destoryed :當前組件已被刪除,清空相關內容
參考文獻
https://segmentfault.com/a/1190000008010666
http://www.cnblogs.com/gagag/p/6246493.html
查看跟多內容,請訪問我的獨立博客 https://www.aaz5.com
