vue前端開發(fā)keepAlive使用詳解
前言
keep-alive 是 Vue 的內(nèi)置組件,當(dāng)它包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,而不是銷毀它們。
在組件切換過程中將狀態(tài)保留在內(nèi)存中,防止重復(fù)渲染DOM,減少加載時(shí)間及性能消耗,提高用戶體驗(yàn)性。使用方式為
<keep-alive>
<component />
</keep-alive>
這里的component會(huì)被緩存。
keep-avlive鉤子函數(shù)
被包含在 keep-alive 中創(chuàng)建的組件,會(huì)多出兩個(gè)生命周期的鉤子: activated與deactivated。activated:在 keep-alive 組件激活時(shí)調(diào)用,keep-alive 會(huì)將數(shù)據(jù)保留在內(nèi)存中,如果要在每次進(jìn)入頁面的時(shí)候獲取最新的數(shù)據(jù),需要在 activated 階段獲取數(shù)據(jù),承擔(dān)原來 created 鉤子函數(shù)中獲取數(shù)據(jù)的任務(wù)
deactivated:在 keep-alive 組件停用時(shí)調(diào)用,使用了keep-alive就不會(huì)調(diào)用beforeDestory和destoryed鉤子,因?yàn)榻M件沒有被銷毀,而是被緩存起來了,所以deactivated鉤子可以看做是beforeDestory和destoryed的替代,如清空localStorge數(shù)據(jù)等。
keep-avlive緩存哪些組件
keep-avlive緩存哪些組件通過兩種方式,一種是通過keep-avlive組件提供的include、exclude屬性通過參數(shù)進(jìn)行匹配對(duì)應(yīng)的組件進(jìn)行緩存,另一種通過route中meta屬性的設(shè)置。
使用include、exclude屬性完成緩存組件設(shè)置
/*將緩存 name 為 test 的組件*/
<keep-alive include='test'>
<router-view/>
</keep-alive>
使用include是將緩存name為test的組件。
<keep-alive exclude="test"> <router-view/> </keep-alive>
使用exclude,將不緩存name為test的組件。
使用route中meta屬性的設(shè)置緩存組件,如
export default new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: Home,
redirect: 'goods',
children: [
{
path: 'goods',
name: 'goods',
component: Goods,
meta: {
keepAlive: false // 不需要緩存
}
},
{
path: 'ratings',
name: 'ratings',
component: Ratings,
meta: {
keepAlive: true // 需要緩存
}
}
]
}
]
})
goods組件需要緩存,ratings不需要緩存。在使用 到中使用以下語句動(dòng)態(tài)完成組件緩存設(shè)置,設(shè)置代碼如下
<template>
<div id="app">
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>
示例
設(shè)置兩個(gè)組件KeepCom1,KeepCom2,KeepCom1設(shè)置緩存,KeepCom2不設(shè)置緩存。同時(shí)測(cè)試兩個(gè)鉤子函數(shù)的使用,這里使用路由meta實(shí)現(xiàn)緩存組件的設(shè)置。
KeepCom1代碼如下:
<template>
<div class="wrapper">
<ul class="content"></ul>
<button class="add" id="add" @click="add">添加子元素</button>
</div>
</template>
<script>
export default {
name: 'keepCom1',
methods: {
add () {
let ul = document.getElementsByClassName('content')[0]
let li = document.createElement('li')
li.innerHTML = '我是添加的元素'
ul.appendChild(li)
}
},
activated () {
console.log('緩存activated執(zhí)行')
},
deactivated () {
console.log('緩存deactivated執(zhí)行')
}
}
</script>
<style>
</style>
KeepCom2代碼如下:
<template>
<div class="wrapper">
<ul class="content"></ul>
<button class="add" id="add" @click="add">添加子元素</button>
</div>
</template>
<script>
export default {
name: 'keepCom2',
methods: {
add () {
let ul = document.getElementsByClassName('content')[0]
let li = document.createElement('li')
li.innerHTML = '我是添加的元素'
ul.appendChild(li)
}
},
activated () {
console.log('緩存activated執(zhí)行')
},
deactivated () {
console.log('緩存deactivated執(zhí)行')
}
}
</script>
<style>
</style>
KeepCom1和KeepCom2代碼基本一致,就是給頁面增加結(jié)點(diǎn)。
keepAvliveTest代碼如下
<template>
<div align="center" style="margin-top: 20px;">
<router-link to="/keepAvliveTest/keepcom1">使用緩存</router-link>
<router-link to="/keepAvliveTest/keepcom2">不使用緩存</router-link>
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>
<script>
export default {
name: 'keepAvliveTest'
}
</script>
<style>
</style>
完成keepcom1和keepcom2組件切換,執(zhí)行后的結(jié)果為

keepcom1使用緩存,切換頁面時(shí),上次添加三個(gè)元素還在,而且鉤子函數(shù)得到執(zhí)行。keepcom2沒有使用緩存,切換頁面時(shí),上次添加一個(gè)元素不存在了,恢復(fù)到初始狀態(tài)。而且兩個(gè)鉤子沒有得到執(zhí)行。
小結(jié)及注意事項(xiàng)
在設(shè)置需要緩存的頁面時(shí),推薦使用router中meta標(biāo)簽,這樣代碼的耦合度較低。keep-alive 先匹配被包含組件的 name 字段,如果 name 不可用,則匹配當(dāng)前組件 components 配置中的注冊(cè)名稱。包含在 keep-alive 中,但符合 exclude ,不會(huì)調(diào)用activated和 deactivated
以上就是vue前端開發(fā)keepAlive使用詳解的詳細(xì)內(nèi)容,更多關(guān)于vue前端的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue 輸入電話號(hào)碼自動(dòng)按3-4-4分割功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 輸入電話號(hào)碼自動(dòng)按3-4-4分割功能的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Vue2.0基于vue-cli+webpack父子組件通信(實(shí)例講解)
下面小編就為大家?guī)硪黄猇ue2.0基于vue-cli+webpack父子組件通信(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Vue 實(shí)現(xiàn)穿梭框功能的詳細(xì)代碼
本文給大家介紹Vue 實(shí)現(xiàn)穿梭框功能,代碼分為css,html和js代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-10-10
使用ElementUI中el-upload上傳文件轉(zhuǎn)base64格式
這篇文章主要介紹了使用ElementUI中el-upload上傳文件轉(zhuǎn)base64格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
基于vue開發(fā)的在線付費(fèi)課程應(yīng)用過程
這篇文章主要介紹了基于vue開發(fā)的在線付費(fèi)課程應(yīng)用過程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
用Axios Element實(shí)現(xiàn)全局的請(qǐng)求loading的方法
本篇文章主要介紹了用Axios Element實(shí)現(xiàn)全局的請(qǐng)求loading的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Vue mixin實(shí)現(xiàn)組件功能復(fù)用示例詳解
這篇文章主要為大家介紹了Vue mixin實(shí)現(xiàn)組件功能復(fù)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

