vue前端開發(fā)keepAlive使用詳解
前言
keep-alive 是 Vue 的內(nèi)置組件,當(dāng)它包裹動態(tài)組件時,會緩存不活動的組件實例,而不是銷毀它們。
在組件切換過程中將狀態(tài)保留在內(nèi)存中,防止重復(fù)渲染DOM,減少加載時間及性能消耗,提高用戶體驗性。使用方式為
<keep-alive>
<component />
</keep-alive>
這里的component會被緩存。
keep-avlive鉤子函數(shù)
被包含在 keep-alive 中創(chuàng)建的組件,會多出兩個生命周期的鉤子: activated與deactivated。activated:在 keep-alive 組件激活時調(diào)用,keep-alive 會將數(shù)據(jù)保留在內(nèi)存中,如果要在每次進(jìn)入頁面的時候獲取最新的數(shù)據(jù),需要在 activated 階段獲取數(shù)據(jù),承擔(dān)原來 created 鉤子函數(shù)中獲取數(shù)據(jù)的任務(wù)
deactivated:在 keep-alive 組件停用時調(diào)用,使用了keep-alive就不會調(diào)用beforeDestory和destoryed鉤子,因為組件沒有被銷毀,而是被緩存起來了,所以deactivated鉤子可以看做是beforeDestory和destoryed的替代,如清空localStorge數(shù)據(jù)等。
keep-avlive緩存哪些組件
keep-avlive緩存哪些組件通過兩種方式,一種是通過keep-avlive組件提供的include、exclude屬性通過參數(shù)進(jìn)行匹配對應(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不需要緩存。在使用 到中使用以下語句動態(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è)置兩個組件KeepCom1,KeepCom2,KeepCom1設(shè)置緩存,KeepCom2不設(shè)置緩存。同時測試兩個鉤子函數(shù)的使用,這里使用路由meta實現(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é)點。
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ù)得到執(zhí)行。keepcom2沒有使用緩存,切換頁面時,上次添加一個元素不存在了,恢復(fù)到初始狀態(tài)。而且兩個鉤子沒有得到執(zhí)行。
小結(jié)及注意事項
在設(shè)置需要緩存的頁面時,推薦使用router中meta標(biāo)簽,這樣代碼的耦合度較低。keep-alive 先匹配被包含組件的 name 字段,如果 name 不可用,則匹配當(dāng)前組件 components 配置中的注冊名稱。包含在 keep-alive 中,但符合 exclude ,不會調(diào)用activated和 deactivated
以上就是vue前端開發(fā)keepAlive使用詳解的詳細(xì)內(nèi)容,更多關(guān)于vue前端的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue 輸入電話號碼自動按3-4-4分割功能的實現(xiàn)代碼
這篇文章主要介紹了vue 輸入電話號碼自動按3-4-4分割功能的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Vue2.0基于vue-cli+webpack父子組件通信(實例講解)
下面小編就為大家?guī)硪黄猇ue2.0基于vue-cli+webpack父子組件通信(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
使用ElementUI中el-upload上傳文件轉(zhuǎn)base64格式
這篇文章主要介紹了使用ElementUI中el-upload上傳文件轉(zhuǎn)base64格式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
用Axios Element實現(xiàn)全局的請求loading的方法
本篇文章主要介紹了用Axios Element實現(xiàn)全局的請求loading的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Vue mixin實現(xiàn)組件功能復(fù)用示例詳解
這篇文章主要為大家介紹了Vue mixin實現(xiàn)組件功能復(fù)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

