Vue生命周期介紹和鉤子函數(shù)詳解
Vue生命周期介紹和鉤子函數(shù)
組件每個階段它的內(nèi)部構(gòu)造是不一樣的,所以一般特定的鉤子做特定的事,比如Ajax獲取數(shù)據(jù)就可以在mounted階段。從Vue實例被創(chuàng)建開始到該實例最終被銷毀的整個過程叫做VUE的生命周期,在這個生命周期內(nèi)發(fā)生了下面的事情:從vue實例被創(chuàng)建開始,首先vue實例被創(chuàng)建,之后開始數(shù)據(jù)的初始化,編譯模板,掛載dom,渲染dom,更新對象屬性,渲染dom,解綁銷毀。
VUE生命周期鉤子
生命周期鉤子又被叫做生命周期時間,生命周期函數(shù),生命周期鉤子就是vue生命周期中出發(fā)的各類事件,這些事件被稱為生命周期鉤子,在vue生命周期中,大部分分為四個階段,創(chuàng)建,掛在,更新,銷毀,這四個階段各自對應(yīng)兩個生命周期鉤子
一、創(chuàng)建部分(create),就是vue實例被初始化,簡單的說就是我們在代碼中進(jìn)行了這個操作 var app = new Vue() ,之后就進(jìn)入的vue生命周期的創(chuàng)建階段。在創(chuàng)建階段中會有兩個提供給我們可編程的接口,分別是beforeCreate 和 created。 這兩個接口都是在創(chuàng)建階段觸發(fā),但是兩個接口又有所不同,beforeCreate比created要先觸發(fā),即vue實例初始化后,沒有進(jìn)行數(shù)據(jù)讀取前就觸發(fā),如果在此時進(jìn)行讀取data中的數(shù)據(jù)就會提示unfined。 created 是在實例創(chuàng)建完成后再被調(diào)用,此時data中的數(shù)據(jù)已經(jīng)寫入完成,但是還沒有進(jìn)行dom的掛載,也就還沒有與頁面進(jìn)行關(guān)聯(lián),下面進(jìn)入掛載階段。
二、掛載階段(mount),該階段就是將頁面中dom掛載到實例化后的vue對象上。簡單的說就是執(zhí)行了 el: ‘#dom'。該階段同樣有兩個接口供我們進(jìn)行編程,分別是 beforemount 和 mounted 。這兩個接口的主要區(qū)別在與以有沒有將dom掛載到實例對象上,beforemount 是在掛載之前觸發(fā),僅對模板進(jìn)行解析,如果此時輸出需要掛載的dom的innerHTML的話,會將模板原樣輸出,并沒有將數(shù)據(jù)進(jìn)行渲染。 mounted 實在dom掛載之后,可以將data中的數(shù)據(jù)渲染的頁面上。該階段之后就進(jìn)入更新階段。
三、更新階段(update),該階段是在頁面上的數(shù)據(jù)在第一次加載之后再次進(jìn)行更新的時候。同樣對應(yīng)兩個接口 beforeupdate 和 update。這兩個接口的差異主要在于有沒有對頁面dom進(jìn)行渲染。在我們對data中的數(shù)據(jù)進(jìn)行修改,且修改完成后觸發(fā) beforeupdate ,此時data中的屬性值已經(jīng)是修改完成的狀態(tài),但是沒有對頁面的dom進(jìn)行渲染。update 就是將數(shù)據(jù)渲染到頁面上。至此vue的生命周期已經(jīng)進(jìn)行到更新階段,在正常的使用中我們會多次的經(jīng)常處于更新階段,對頁面的數(shù)據(jù)進(jìn)行各種修改。但是為了滿足將不必要的事件關(guān)閉,釋放內(nèi)存,就還需要銷毀階段。
四、銷毀階段(destory),在一個實列被銷毀后,該實例所綁定的所有事件都會失效,監(jiān)聽器也會被移出。該階段對應(yīng)兩個接口 beforeDestroy 和 destroy。deforeDestory是在實例需要被銷毀但是還沒有先回之前調(diào)用,此時該實例的的綁定的各類事件、監(jiān)聽器仍然可用。 destroy是在實例銷毀后調(diào)用,此時該實例的所有東西都不能使用,但是頁面上的數(shù)據(jù)仍保持頁面最后一次渲染的數(shù)據(jù)。
Vue生命周期簡介
上面描述的幾個階段
使用代碼觀察鉤子函數(shù)
<!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)
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掛載的時候再把值渲染進(jìn)去。
update 相關(guān)
destroy 相關(guān)
有關(guān)于銷毀,暫時還不是很清楚。我們在console里執(zhí)行下命令對 vue實例進(jìn)行銷毀。銷毀完成后,我們再重新改變message的值,vue不再對此動作進(jìn)行響應(yīng)了。但是原先生成的dom元素還存在,可以這么理解,執(zhí)行了destroy操作,后續(xù)就不再受vue控制了。
總結(jié)
beforecreate :
舉個例子:可以在這加個loading事件
created :
在這結(jié)束loading,還做一些初始化,實現(xiàn)函數(shù)自執(zhí)行
mounted :
在這發(fā)起后端請求,拿回數(shù)據(jù),配合路由鉤子做一些事情
beforeDestory:
你確認(rèn)刪除XX嗎? destoryed :當(dāng)前組件已被刪除,清空相關(guān)內(nèi)容
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue調(diào)用swiper插件步驟教程(最易理解且詳細(xì))
有時候我們需要在vue中使用輪播組件,如果是在vue組件中引入第三方組件的話,最好通過npm安裝,從而進(jìn)行統(tǒng)一安裝包管理,下面這篇文章主要給大家介紹了關(guān)于vue調(diào)用swiper插件的相關(guān)資料,需要的朋友可以參考下2023-04-04vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較
這篇文章主要為大家介紹了vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Vue+Element-ui彈窗?this.$alert?is?not?a?function問題
這篇文章主要介紹了Vue+Element-ui彈窗?this.$alert?is?not?a?function問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進(jìn)度條效果
這篇文章主要介紹了Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進(jìn)度條效果,通過實例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07