Vue 2.0 個人關于生命周期的理解

在使用vue一個多禮拜后,感覺現(xiàn)在還停留在初級階段,雖然知道怎么和后端做數(shù)據(jù)交互,但是對于mounted這個掛載還不是很清楚的。放大之,對vue的生命周期不甚了解。只知道簡單的使用,而不知道為什么,這對后面的踩坑是相當不利的。

因為我們有時候會在幾個鉤子函數(shù)里做一些事情,什么時候做,在哪個函數(shù)里做,我們不清楚。

于是我開始先去搜索,發(fā)現(xiàn)vue2.0的生命周期沒啥文章。大多是1.0的版本介紹。最后還是找到一篇不錯的(會放在最后)

vue生命周期簡介

咱們從上圖可以很明顯的看出現(xiàn)在vue2.0都包括了哪些生命周期的函數(shù)了。

生命周期探究

對于執(zhí)行順序和什么時候執(zhí)行,看上面兩個圖基本有個了解了。下面我們將結合代碼去看看鉤子函數(shù)的執(zhí)行。

ps:下面代碼可以直接復制出去執(zhí)行

{{ message }}

varapp =newVue({el:'#app',data: {message:"xuxiao is boy"},beforeCreate:function(){console.group('beforeCreate 創(chuàng)建前狀態(tài)===============》');console.log("%c%s","color:red","el? ? : "+this.$el);//undefinedconsole.log("%c%s","color:red","data? : "+this.$data);//undefined console.log("%c%s","color:red","message: "+this.message)? ? ? ? ? },created:function(){console.group('created 創(chuàng)建完畢狀態(tài)===============》');console.log("%c%s","color:red","el? ? : "+this.$el);//undefinedconsole.log("%c%s","color:red","data? : "+this.$data);//已被初始化 console.log("%c%s","color:red","message: "+this.message);//已被初始化},beforeMount:function(){console.group('beforeMount 掛載前狀態(tài)===============》');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 掛載結束狀態(tài)===============》');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 更新前狀態(tài)===============》');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 更新完成狀態(tài)===============》');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 銷毀前狀態(tài)===============》');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 銷毀完成狀態(tài)===============》');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)? ? ? ? }? ? })

create 和 mounted 相關

咱們在chrome瀏覽器里打開,F(xiàn)12看console就能發(fā)現(xiàn)

beforecreated:el 和 data 并未初始化?

created:完成了 data 數(shù)據(jù)的初始化,el沒有

beforeMount:完成了 el 和 data 初始化?

mounted?:完成掛載

另外在標紅處,我們能發(fā)現(xiàn)el還是 {{message}},這里就是應用的?Virtual DOM(虛擬Dom)技術,先把坑占住了。到后面mounted掛載的時候再把值渲染進去。

update 相關

這里我們在 chrome console里執(zhí)行以下命令

app.message=?'yes?!! I?do';

下面就能看到data里的值被修改后,將會觸發(fā)update的操作。

destroy 相關

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

app.$destroy();

生命周期總結

這么多鉤子函數(shù),我們怎么用呢,我想大家可能有這樣的疑問吧,我也有,哈哈哈。

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

created?:在這結束loading,還做一些初始化,實現(xiàn)函數(shù)自執(zhí)行?

mounted?: 在這發(fā)起后端請求,拿回數(shù)據(jù),配合路由鉤子做一些事情

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容