轉載:http://blog.csdn.net/qq_15766181/article/details/73549933javascript
鉤子就好像是把人的出生到死亡分紅一個個階段,你確定是在出生階段起名字,而不會在成年或者死亡的階段去起名字。或者說你想在出生階段去約炮,也是不行的。組件也是同樣,每一個階段它的內部構造是不同的。因此通常特定的鉤子作特定的事,好比ajax獲取數據就能夠在mounted階段。css
我們從上圖能夠很明顯的看出如今vue2.0都包括了哪些生命週期的函數了,總結一下,對官方文檔的那張圖簡化一下,就獲得了這張圖。html
對於執行順序和何時執行,看上面圖基本有個瞭解了。下面咱們將結合代碼去看看鉤子函數的執行。vue
<!DOCTYPE html> <html> <head> <title>鉤子函數</title> <meta charset="utf-8"> <script src="http://cdn.bootcss.com/vue/2.1.10/vue.js"></script> <body> <div id="app"> <p>{{ message }}</p> <input type="button" @click="change" value="更新數據" /> <input type="button" @click="destroy" value="銷燬" /> </div> <script type="text/javascript"> var vm = new Vue({ el: '#app', data: { message : "Welcome Vue" }, methods:{ change() { this.message = 'Datura is me'; }, destroy() { vm.$destroy(); } }, 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);//undefined }, created: function () { console.group('created 建立完畢狀態===============》'); console.log("%c%s", "color:red","el : " + this.$el); //undefined console.log("%c%s", "color:green","data : " + this.$data); //[object Object] => 已被初始化 console.log("%c%s", "color:green","message: " + this.message); //Welcome Vue => 已被初始化 }, beforeMount: function () { console.group('beforeMount 掛載前狀態===============》'); console.log("%c%s", "color:green","el : " + (this.$el)); //已被初始化 console.log(this.$el); // 當前掛在的元素 console.log("%c%s", "color:green","data : " + this.$data); //已被初始化 console.log("%c%s", "color:green","message: " + this.message); //已被初始化 }, mounted: function () { console.group('mounted 掛載結束狀態===============》'); console.log("%c%s", "color:green","el : " + this.$el); //已被初始化 console.log(this.$el); console.log("%c%s", "color:green","data : " + this.$data); //已被初始化 console.log("%c%s", "color:green","message: " + this.message); //已被初始化 }, beforeUpdate: function () { alert("更新前狀態"); console.group('beforeUpdate 更新前狀態===============》'); //這裏指的是頁面渲染新數據以前 console.log("%c%s", "color:green","el : " + this.$el); console.log(this.$el); console.log("%c%s", "color:green","data : " + this.$data); console.log("%c%s", "color:green","message: " + this.message); alert("更新前狀態2"); }, updated: function () { console.group('updated 更新完成狀態===============》'); console.log("%c%s", "color:green","el : " + this.$el); console.log(this.$el); console.log("%c%s", "color:green","data : " + this.$data); console.log("%c%s", "color:green","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>
beforecreated:el 和 data 並未初始化
created:完成了 data 數據的初始化,el沒有
beforeMount:完成了 el 和 data 初始化
mounted :完成掛載java
另外在標紅處,咱們能發現el仍是 {{message}},這裏就是應用的 Virtual DOM(虛擬Dom)技術,先把坑佔住了。到後面mounted掛載的時候再把值渲染進去。ajax
咱們單擊頁面中的「更新數據」按鈕,將數據更新。下面就能看到data裏的值被修改後,將會觸發update的操做。django
銷燬完成後,咱們再從新改變message的值,vue再也不對此動做進行響應了。可是原先生成的dom元素還存在,能夠這麼理解,執行了destroy操做,後續就再也不受vue控制了。由於這個Vue實例已經不存在了。
咱們單擊頁面中的「銷燬」按鈕,將指定的Vue實例銷燬。後端
beforecreate : 舉個栗子:能夠在這加個loading事件
created :在這結束loading,還作一些初始化,實現函數自執行
mounted : 在這發起後端請求,拿回數據,配合路由鉤子作一些事情
beforeDestory: 你確認刪除XX嗎? destoryed :當前組件已被刪除,清空相關內容app