Vue 2.0中生命周期與鉤子函數(shù)的一些理解
前言
在使用vue一個(gè)多禮拜后,感覺現(xiàn)在還停留在初級(jí)階段,雖然知道怎么和后端做數(shù)據(jù)交互,但是對(duì)于mounted這個(gè)掛載還不是很清楚的。放大之,對(duì)vue的生命周期不甚了解。只知道簡(jiǎn)單的使用,而不知道為什么,這對(duì)后面的踩坑是相當(dāng)不利的。
因?yàn)槲覀冇袝r(shí)候會(huì)在幾個(gè)鉤子函數(shù)里做一些事情,什么時(shí)候做,在哪個(gè)函數(shù)里做,我們不清楚。
于是我開始先去搜索,發(fā)現(xiàn)vue2.0的生命周期沒啥文章。大多是1.0的版本介紹。最后還是找到一篇不錯(cuò)的(會(huì)放在最后)
vue生命周期簡(jiǎn)介


咱們從上圖可以很明顯的看出現(xiàn)在vue2.0都包括了哪些生命周期的函數(shù)了。
生命周期探究
對(duì)于執(zhí)行順序和什么時(shí)候執(zhí)行,看上面兩個(gè)圖基本有個(gè)了解了。下面我們將結(jié)合代碼去看看鉤子函數(shù)的執(zhí)行。
ps:下面代碼可以直接復(fù)制出去執(zhí)行
<!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 創(chuàng)建前狀態(tài)===============》');
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 創(chuàng)建完畢狀態(tài)===============》');
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 掛載前狀態(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 掛載結(jié)束狀態(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)
}
})
</script>
</body>
</html>
create 和 mounted 相關(guān)
咱們?cè)赾hrome瀏覽器里打開,F(xiàn)12看console就能發(fā)現(xiàn)
- beforecreated:el 和 data 并未初始化
- created:完成了 data 數(shù)據(jù)的初始化,el沒有
- beforeMount:完成了 el 和 data 初始化
- mounted :完成掛載
另外在標(biāo)紅處,我們能發(fā)現(xiàn)el還是 {{message}} ,這里就是應(yīng)用的 Virtual DOM(虛擬Dom)技術(shù),先把坑占住了。到后面mounted掛載的時(shí)候再把值渲染進(jìn)去。

update 相關(guān)
這里我們?cè)?chrome console里執(zhí)行以下命令
app.message= 'yes !! I do';
下面就能看到data里的值被修改后,將會(huì)觸發(fā)update的操作。

destroy 相關(guān)
有關(guān)于銷毀,暫時(shí)還不是很清楚。我們?cè)赾onsole里執(zhí)行下命令對(duì) vue實(shí)例進(jìn)行銷毀。銷毀完成后,我們?cè)僦匦赂淖僲essage的值,vue不再對(duì)此動(dòng)作進(jìn)行響應(yīng)了。但是原先生成的dom元素還存在,可以這么理解,執(zhí)行了destroy操作,后續(xù)就不再受vue控制了。
app.$destroy();

生命周期總結(jié)
這么多鉤子函數(shù),我們?cè)趺从媚?,我想大家可能有這樣的疑問吧,我也有,哈哈哈。
- beforecreate : 舉個(gè)栗子:可以在這加個(gè)loading事件
- created :在這結(jié)束loading,還做一些初始化,實(shí)現(xiàn)函數(shù)自執(zhí)行
- mounted : 在這發(fā)起后端請(qǐng)求,拿回?cái)?shù)據(jù),配合路由鉤子做一些事情
- beforeDestory: 你確認(rèn)刪除XX嗎? destoryed :當(dāng)前組件已被刪除,清空相關(guān)內(nèi)容
當(dāng)然,還有更多,繼續(xù)探索中......
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
如何通過Vue自帶服務(wù)器實(shí)現(xiàn)Ajax請(qǐng)求跨域(vue-cli)
從A頁面訪問到B頁面,并且要獲取到B頁面上的數(shù)據(jù),而兩個(gè)頁面所在的端口、協(xié)議和域名中哪怕有一個(gè)不對(duì)等,那么這種行為就叫跨域,這篇文章給大家介紹如何通過Vue自帶服務(wù)器實(shí)現(xiàn)Ajax請(qǐng)求跨域(vue-cli),感興趣的朋友一起看看吧2023-10-10
Vue2?中自定義圖片懶加載指令?v-lazy實(shí)例詳解
這篇文章主要為大家介紹了Vue2?中自定義圖片懶加載指令?v-lazy實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)
這篇文章主要介紹了vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue實(shí)現(xiàn)多個(gè)tab標(biāo)簽頁的切換與關(guān)閉詳細(xì)代碼
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)多個(gè)tab標(biāo)簽頁的切換與關(guān)閉的相關(guān)資料,使用vue.js實(shí)現(xiàn)tab切換很簡(jiǎn)單,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
vue2.0如何借用vue-pdf實(shí)現(xiàn)在線預(yù)覽pdf文件
這篇文章主要介紹了vue2.0如何借用vue-pdf實(shí)現(xiàn)在線預(yù)覽pdf文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue循環(huán)中點(diǎn)擊選中再點(diǎn)擊取消(單選)的實(shí)現(xiàn)
這篇文章主要介紹了vue循環(huán)中點(diǎn)擊選中再點(diǎn)擊取消(單選)的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09

