vue - 生命周期
阿新 • • 發佈:2018-09-09
group 激活 ads 開始 ole end 好的 更新 undefined
圖例
官網vue生命周期鉤子.
生命周期 說明
beforeCreate | 在實例初始化之後,數據觀測和事件配置之前被調用 |
created | 在實例創建完成後被立即調用,完成數據觀測,屬性和方法的運算,初始化事件,$el屬性未見 |
beforeMount | 在掛載開始之前被調用:相關的 render 函數首次被調用,只在虛擬DOM生成HTML |
mounted | 在el 被新創建的 vm.$el 替換,並掛載到實例上去之後調用。實例已完成以下的配置:用上面編譯好的html內容替換el屬性指向的DOM對象。完成模板中的html渲染到html頁面中。此過程中進行ajax交互 |
beforeUpdate | 在數據更新之前調用,發生在虛擬DOM重新渲染和打補丁之前。可以在該鉤子中進一步地更改狀態,不會觸發附加的重渲染過程 |
updated | 在由於數據更改導致的虛擬DOM重新渲染和打補丁之後調用。調用時,組件DOM已經更新,所以可以執行依賴於DOM的操作。然而在大多數情況下,應該避免在此期間更改狀態,因為這可能會導致更新無限循環。該鉤子在服務器端渲染期間不被調用 |
activated | keep-alive 組件激活時調用 |
deactivated | keep-alive 組件停用時調用 |
beforeDestroy | 在實例銷毀之前調用。實例仍然完全可用 |
destroyed | 在實例銷毀之後調用。調用後,所有的事件監聽器會被移除,所有的子實例也會被銷毀。該鉤子在服務器端渲染期間不被調用 |
生命周期鉤子示例代碼
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Vue入門之Helloworld</title>
6 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js" ></script>
7 </head>
8 <body>
9 <div id="app">
10 {{message}}
11 </div>
12
13 <script type="text/javascript">
14 var app=new Vue({
15 el:‘#app‘,
16 data:{
17 message:‘hello Vue!‘
18 },
19 beforeCreate: function () {
20 console.group(‘beforeCreate 創建前狀態===============》‘);
21 console.log("%c%s", "color:red" , "el : " + this.$el); //undefined
22 console.log("%c%s", "color:red","data : " + this.$data); //undefined
23 console.log("%c%s", "color:red","message: " + this.message)
24 },
25 created: function () {
26 console.group(‘created 創建完畢狀態===============》‘);
27 console.log("%c%s", "color:red","el : " + this.$el); //undefined
28 console.log("%c%s", "color:red","data : " + this.$data); //已被初始化
29 console.log("%c%s", "color:red","message: " + this.message); //已被初始化
30 },
31 beforeMount: function () {
32 console.group(‘beforeMount 掛載前狀態===============》‘);
33 console.log("%c%s", "color:red","el : " + (this.$el)); //已被初始化
34 console.log(this.$el);
35 console.log("%c%s", "color:red","data : " + this.$data); //已被初始化
36 console.log("%c%s", "color:red","message: " + this.message); //已被初始化
37 },
38 mounted: function () {
39 console.group(‘mounted 掛載結束狀態===============》‘);
40 console.log("%c%s", "color:red","el : " + this.$el); //已被初始化
41 console.log(this.$el);
42 console.log("%c%s", "color:red","data : " + this.$data); //已被初始化
43 console.log("%c%s", "color:red","message: " + this.message); //已被初始化
44 },
45 beforeUpdate: function () {
46 console.group(‘beforeUpdate 更新前狀態===============》‘);
47 console.log("%c%s", "color:red","el : " + this.$el);
48 console.log(this.$el);
49 console.log("%c%s", "color:red","data : " + this.$data);
50 console.log("%c%s", "color:red","message: " + this.message);
51 },
52 updated: function () {
53 console.group(‘updated 更新完成狀態===============》‘);
54 console.log("%c%s", "color:red","el : " + this.$el);
55 console.log(this.$el);
56 console.log("%c%s", "color:red","data : " + this.$data);
57 console.log("%c%s", "color:red","message: " + this.message);
58 },
59 beforeDestroy: function () {
60 console.group(‘beforeDestroy 銷毀前狀態===============》‘);
61 console.log("%c%s", "color:red","el : " + this.$el);
62 console.log(this.$el);
63 console.log("%c%s", "color:red","data : " + this.$data);
64 console.log("%c%s", "color:red","message: " + this.message);
65 },
66 destroyed: function () {
67 console.group(‘destroyed 銷毀完成狀態===============》‘);
68 console.log("%c%s", "color:red","el : " + this.$el);
69 console.log(this.$el);
70 console.log("%c%s", "color:red","data : " + this.$data);
71 console.log("%c%s", "color:red","message: " + this.message)
72 }
73 })
74 </script>
75 </body>
76 </html>
在chrome瀏覽器裏打開,F12看console查看,分三個階段解讀:
階段一:創建和掛載
- beforecreated:el 和 data 並未初始化
- created:完成了 data 數據的初始化,el沒有
- beforeMount:完成了 el 和 data 初始化
- mounted :完成掛載
階段二:更新
在chrome console執行以下命令:
app.message= ‘yes !! I do‘;
- beforeUpdate:虛擬DOM中根據data變化去更新html
- updated:將虛擬DOM更新完成的HTML更新到頁面中
階段三:銷毀
在chrome console執行以下命令:
app.$destroy();
- beforeDestroy:銷毀之前調用
- destroyed:銷毀之後調用,之後再執行app.message= ‘hello vue’,頁面不會同步更新。
具體圖解如下:
vue - 生命周期