Vue 2.0 淺談--生命周期和鉤子函數

cvhb6863 7年前發布 | 43K 次閱讀 Vue.js Vue.js開發

前言

用Vue也有一段時間了,發現生命周期是很重要的一部分,稍微懂得了一些東西,特地來分享一下.

生命周期和鉤子函數-介紹

啥也不說先上圖

圖-1為 Vue 1.0 生命周期圖,圖-2為 Vue 2.0 生命周期圖,圖-3為Vue 1.0 和 Vue 2.0 鉤子函數比較

重點看 Vue 2.0

生命周期和鉤子函數-具體

上代碼

自己粘走執行

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src=";
</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></code></pre>

打開F12可以查看生命周期各個時期的鉤子函數的狀態,如下圖

由上圖知:

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 = '數據更新'后

如下圖

由此可見,當data數據變化時只會觸發update

Vue實例解耦(destroy)

在控制臺里輸入app.$destroy();

如下圖

由圖知:

執行完destroy操作后,data里的數據沒有變化,但是Dom結構還存在,也就是Vue實例不再受控制了,完成了解耦

生命周期和鉤子函數-總結

如下圖

這是把官方 Vue 2.0 生命周期的圖例簡化后的

生命周期鉤子函數使用

beforecreate : 舉個栗子:可以在這加個loading事件

created :在這結束loading,還做一些初始化,實現函數自執行

mounted : 在這發起后端請求,拿回數據,配合路由鉤子做一些事情

beforeDestory: 你確認刪除XX嗎? destoryed :當前組件已被刪除,清空相關內容

 

來自:https://segmentfault.com/a/1190000009677699

 

 本文由用戶 cvhb6863 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!