Vue組件生命周期三個階段全面總結(jié)講解
組件生命周期
生命周期(Life Cycle)是指一個組件從 創(chuàng)建 -> 運(yùn)行 -> 銷毀 的整個階段,強(qiáng)調(diào)的是一個時間段。
生命周期函數(shù):是由 vue 框架提供的內(nèi)置函數(shù),會伴隨著組件的生命周期,自動按次序執(zhí)行。

創(chuàng)建階段
beforeCreate階段
我們在初始化事件和生命周期函數(shù)時,組件的 props/data/methods尚未被創(chuàng)建,都處于不可用狀態(tài)。
<script>
export default {
props:['info'],
data(){
return {
message:'hello test'
}
},
methods:{
show(){
console.log('調(diào)用了 Test 組件的 show 方法');
}
},
// 創(chuàng)建階段的第一個生命周期
beforeCreate(){
console.log(this.info); //props
console.log(this.message); //data
this.show() //methods
},
}
</script>因為不能使用 props/data/methods 但是我調(diào)用了,所有控制臺報錯。

created階段
我們已經(jīng)初始化好 props、data、methods了,組件的 props/data/methods已創(chuàng)建好。都處于可用狀態(tài),但是組件的模板結(jié)構(gòu)尚未完成!
<script>
export default {
props:['info'],
data(){
return {
message:'hello test'
}
},
methods:{
show(){
console.log('調(diào)用了 Test 組件的 show 方法');
}
},
// 創(chuàng)建階段的第二個生命周期函數(shù)
created(){
console.log(this.info);
console.log(this.message);
this.show()
}
}
</script>
created生命周期函數(shù)非常常用,在日常項目開發(fā)中經(jīng)常在它里面調(diào)用 methods 中的方法,請求服務(wù)器的數(shù)據(jù),并且把請求到的數(shù)據(jù),轉(zhuǎn)存到 data 中,供 template 模板渲染的時候使用!
<template>
<div>
<h2>test組件--{{nums.length}}</h2>
</div>
</template>
<script>
export default {
props:['info'],
data(){
return {
message:'hello test',
nums:[]
}
},
methods:{
show(){
console.log('調(diào)用了 Test 組件的 show 方法');
},
initlist(){
const xhr = new XMLHttpRequest()
xhr.addEventListener('load',()=>{
const result = JSON.parse(xhr.responseText)
console.log(result);
this.nums = result.data
})
xhr.open('GET','請求的接口')
xhr.send()
}
},
created(){
this.initlist()
}
}
</script>
<style lang="less" scoped>
div{
background-color: #f00;
}
</style>beforeMount階段
基于數(shù)據(jù)和模板,在內(nèi)存中編譯生成HTML結(jié)構(gòu)。將要把內(nèi)存中編譯好的HTML結(jié)構(gòu)渲染到瀏覽器中。此時瀏覽器中還沒有當(dāng)前組件的DOM結(jié)構(gòu)。
<template>
<div>
<h2 id="myid">test組件--{{nums.length}}</h2>
</div>
</template>
<script>
export default {
props:['info'],
data(){},
methods:{},
beforeCreate(){},
created(){},
beforeMount(){
console.log('beforeMount');
const dom = document.querySelector('#myid')
console.log(dom);
}
}
</script>
mounted階段
用內(nèi)存中編譯生成的HTML結(jié)構(gòu)替換掉el屬性指定的DOM元素,已經(jīng)把內(nèi)存中的HTML結(jié)構(gòu),成功渲染到了瀏覽器之中,此時瀏覽器中已經(jīng)包含了當(dāng)前組件的DOM結(jié)構(gòu)。
<template>
<div>
<h2 id="myid">test組件--{{nums.length}}</h2>
</div>
</template>
<script>
export default {
mounted(){
const dom = document.querySelector('#myid')
console.log(dom);
}
}
</script>
vue完成模板解析并把初識的真實DOM元素放在頁面后(掛載完畢)調(diào)用 mounted。
<template>
<div>
<h2 :style="{opacity}">歡迎學(xué)習(xí)Vue</h2>
</div>
</template>
<script>
export default {
data(){
return {
opacity:1
}
},
mounted(){
setInterval(()=>{
//我們在使用箭頭函數(shù)時,this的指向mounted自動幫我們設(shè)置好是 vm 了
this.opacity-=0.01
if(this.opacity <= 0) this.opacity = 1
},6)
},
}
</script>
運(yùn)行階段
所謂運(yùn)行階段就是用戶與組件產(chǎn)生交互
beforeUpdate階段
這個函數(shù)的觸發(fā)的必要前提是,我們修改了 data 里面的數(shù)據(jù)。將要(注意:僅僅是將要,還沒有呢)根據(jù)變化過后最新的數(shù)據(jù),重新渲染組件的模板結(jié)構(gòu)。
<template>
<div>
<h2 id="myid">{{message}}</h2>
<button @click="message+='~'">點擊修改message值</button>
</div>
</template>
<script>
export default {
data(){
return {
message:'hello test',
}
},
beforeUpdate(){
console.log('beforeUpdate'); //說明:點擊按鈕修改data值才能觸發(fā)這個函數(shù)
console.log(this.message); //打印修改后的值
const dom = document.querySelector('#myid')
console.log(dom.innerHTML); //打印整個文本,但并沒有出現(xiàn)我們修改后的值,說明頁面并沒有重新渲染
}
}
</script>
updated階段
已經(jīng)根據(jù)最新的數(shù)據(jù),完成了組件的DOM結(jié)構(gòu)的重新渲染。注意:當(dāng)數(shù)據(jù)變化之后,為了能操作到最新的 DOM 結(jié)構(gòu),必須把代碼寫到 updated 生命周期函數(shù)中。
<template>
<div>
<h2 id="myid">{{message}}</h2>
<button @click="message+='~'">點擊修改message值</button>
</div>
</template>
<script>
export default {
props:['info'],
data(){
return {
message:'hello test',
}
},
updated(){
console.log('updated');
console.log(this.message); //打印修改后的值
const dom = document.querySelector('#myid')
console.log(dom.innerHTML); //打印整個文本,但出現(xiàn)了我們修改后的值,說明頁面被重新渲染
}
}
</script>銷毀階段
完全銷毀一個實例。清理它(vm)與其它實例的連接,接綁它的全部指令及事件監(jiān)聽器。
beforeDestroy階段
將要銷毀此組件,此時尚未銷毀,組件還處于正常工作狀態(tài)。在這階段一般做一些首尾工作。
<template>
<div>
<h2 id="myid">{{message}}</h2>
<button @click="message+='~'">點擊修改message值</button>
</div>
</template>
<script>
export default {
props:['info'],
data(){
return {
message:'hello test',
}
},
beforeDestroy(){
console.log('beforeDestroy');
}
}
destroyed階段
銷毀當(dāng)前組件的數(shù)據(jù)偵 聽 器、子組件、事件監(jiān)聽等,組件已經(jīng)被銷毀,此組件在瀏覽器中對應(yīng)的DOM結(jié)構(gòu)已被完全移除!
直接暴力的將vm干掉,頁面就不能再進(jìn)行交互。設(shè)置透明的按鈕也就作廢了。
<template>
<div>
<h2 :style="{opacity}">歡迎學(xué)習(xí)Vue</h2>
<button @click="opacity = 1">透明度設(shè)置為1</button>
<button @click="stop">點我停止變化</button>
</div>
</template>
<script>
export default {
data(){
return {
opacity:1
}
},
methods:{
stop(){
// clearInterval(this.timer)
this.$destroy()
}
},
mounted(){
// const dom = document.querySelector('#myid')
// console.log(dom);
console.log('mounted',this);
this.timer = setInterval(()=>{
console.log('setInterval');
this.opacity-=0.01
if(this.opacity <= 0) this.opacity = 1
},6)
},
beforeDestroy(){
clearInterval(this.timer)
console.log('vm即將被銷毀');
}
}
</script>
<style lang="less" scoped>
div{
// background-color: #f00;
}
</style>
1)銷毀后借助Vue開發(fā)者工具看不到任何信息。
2)銷毀后自定義事件會失效,但原生的DOM事件依然有效
3)一般不會在beforeDestroy操作數(shù)據(jù),因為即使操作數(shù)據(jù),也不會再觸發(fā)更新流程了
總結(jié)
生命周期:
1)又稱:生命周期回調(diào)函數(shù)、生命周期函數(shù)、生命周期鉤子。
2)含義:vue在關(guān)鍵時刻幫助我們調(diào)用一些特殊名稱的函數(shù)。
3)生命周期函數(shù)的名字不可更改,但函數(shù)的具體內(nèi)容是程序員根據(jù)需求編寫的。
4)生命周期函數(shù)中的this指向是 vm 或 組件實例對象。
常用的生命周期鉤子:
1)mounted:發(fā)送ajax請求、啟動定時器、綁定自定義事件、訂閱消息等(初始化操作)
2)beforeDestroy:清除定時器、綁定自定義事件、取消訂閱消息等(收尾工作)
下面是實例生命周期的圖表。你現(xiàn)在并不需要完全理解圖中的所有內(nèi)容,但以后它將是一個有用的參考。

到此這篇關(guān)于Vue組件生命周期三個階段全面總結(jié)講解的文章就介紹到這了,更多相關(guān)Vue組件生命周期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.0 vue.config.js 配置基礎(chǔ)的路徑問題
這篇文章主要介紹了vue3.0 vue.config.js 配置基礎(chǔ)的路徑問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
import.meta.glob() 如何導(dǎo)入多個目錄下的資源(最新推薦)
import.meta.glob() 其實不僅能接收一個字符串,還可以接收一個字符串?dāng)?shù)組,就是匹配多個位置,本文給大家介紹import.meta.glob() 如何導(dǎo)入多個目錄下的資源,感興趣的朋友一起看看吧2023-11-11
解決Mint-ui 框架Popup和Datetime Picker組件滾動穿透的問題
這篇文章主要介紹了解決Mint-ui 框架Popup和Datetime Picker組件滾動穿透的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue項目中實現(xiàn)ElementUI按需引入過程解析
這篇文章主要介紹了Vue項目中實現(xiàn)ElementUI按需引入,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
用Axios Element實現(xiàn)全局的請求loading的方法
本篇文章主要介紹了用Axios Element實現(xiàn)全局的請求loading的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

