先上一張生命週期的圖:javascript
8個生命週期:html
beforeCreate:組件實例剛被建立,組件屬性計算以前 created:組件實例建立完成,屬性已綁定,但DOM還未生成,$el屬性還不存在 beforeMount:模板編譯/掛載以前 mounted:模板編譯/掛載以後 beforeUpdate:組件更新以前 updated:組件更新以後 brforeDestroy:組件銷燬前調用 destroyed:組件銷燬後調用
對於執行順序和何時執行,看上面兩個圖基本有個瞭解了。下面咱們將結合代碼去看看鉤子函數的執行。vue
先上一段代碼:java
<!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>
打印的結果以下:chrome
得出:segmentfault
beforeCreate:el和data並未初始化,app
created:完成data數據的初始化,el沒有dom
beforeMount:完成el和data的初始化,上圖紅框處,發現el仍是{{message}},這就是虛擬DOM技術,先把坑佔住了,到後面mounted的時候再把值渲染進去函數
mounted:完成掛載this
這裏咱們在 chrome console裏執行如下命令:
app.message= 'yes !! I do';
下面就能看到data裏的值被修改後,將會觸發update的操做。
咱們在console裏執行下命令對 vue實例進行銷燬。銷燬完成後,咱們再從新改變message的值,vue再也不對此動做進行響應了。可是原先生成的dom元素還存在,能夠這麼理解,執行了destroy操做,後續就再也不受vue控制了。
app.$destroy();
這麼多鉤子函數,咱們怎麼用呢,我想你們可能有這樣的疑問吧,我也有,哈哈哈。
參考:http://www.cnblogs.com/gagag/p/6246493.html
https://segmentfault.com/q/1010000007704114?_ea=1431323
https://segmentfault.com/a/1190000008010666