Vue keep-alive組件的使用及如何清除緩存
1. keepAlive
在router的js文件中加入keepAlive
{
path: "/A",
name: 'A',
meta:{
name: 'A' ,
keepAlive: false ,
},
component: resolve => require(['../A.vue'], resolve)
},
{
path: "/B",
name: 'B',
meta:{
name: 'B' ,
keepAlive: true ,
},
component: resolve => require(['../B.vue'], resolve)
},
{
path: "/C",
name: 'C',
meta:{
name: 'C' ,
keepAlive: false,
},
component: resolve => require(['../C.vue'], resolve)
},
在APP.vue中加入以下代碼
<keep-alive> <router-view v-if="$route.meta.keepAlive" /> </keep-alive> <router-view v-if="!$route.meta.keepAlive" />
2. 清除緩存的幾種方法
有A,B,C三個(gè)組件需要在B組件中加入緩存,從組件B跳轉(zhuǎn)組件C再從組件C跳轉(zhuǎn)組件B緩存保留,但從組件B跳轉(zhuǎn)到組件A后將組件B的緩存清除。
第一種用直接暴力法,在組件切換之后直接刷新
在組件B中加入beforeRouteLeave()方法和Watch監(jiān)聽事件
watch: {
$route(to, from) {
if(from.name === 'A'){
this.route = from.name
//如果是組件A跳轉(zhuǎn)到的組件B,將組件B刷新
this.$router.go(0);
}
},
},
//組件B跳轉(zhuǎn)后將組件B的 keepAlive 值設(shè)置為false
beforeRouteLeave(to, from, next) {
from.meta.keepAlive = false
next()
}
watch: {
$route(to, from) {
if(from.name === 'A'){
this.route = from.name
//如果是組件A跳轉(zhuǎn)到的組件B,將組件B刷新
this.$router.go(0);
}
},
},
//組件B跳轉(zhuǎn)后將組件B的 keepAlive 值設(shè)置為false
beforeRouteLeave(to, from, next) {
from.meta.keepAlive = false
next()
}
組件B跳轉(zhuǎn)到組件C組件B的緩存不清除,在組件C中加入beforeRouteLeave()方法
beforeRouteLeave (to, from, next) {
//若從組件C跳轉(zhuǎn)到組件B, 將組件B的 keepAlive 值為true
to.meta.keepAlive = true
next()
}
第二種方法用$vnode方法獲取DOM元素,將緩存清除
組件B中加入beforeRouteLeave()方法
beforeRouteLeave(to, from, next) {
if (to.path == "/C") { //這里的路徑是要跳轉(zhuǎn)的需要緩存的路徑
from.meta.keepAlive = true;
} else {
let Vnode = this.$vnode;
let parentVnode = Vnode && Vnode.parent;
if (parentVnode && parentVnode.componentInstance && parentVnode.componentInstance.cache) {
var key = Vnode.key == null ? Vnode.componentOptions.Ctor.cid + (Vnode.componentOptions.tag ? `::${Vnode.componentOptions.tag}` : '') : Vnode.key;
var cache = parentVnode.componentInstance.cache;
var keys = parentVnode.componentInstance.keys;
if (cache[key]) {
this.$destroy();
if (keys.length) {
var index = keys.indexOf(key);
if (index > -1) {
keys.splice(index, 1);
}
}
cache[key] = null;
}
}
}
next();
}
在組件C中加入beforeRouteLeave()和beforeRouteEnter()方法
data(){
return{
Fromvnode: null
}
}
beforeRouteEnter(to, from, next) {
next(vm => {
vm.Fromvnode = from
})
},
beforeRouteLeave(to, from, next) {
if (to.path !== '/B') {
this.Fromvnode.meta.keepAlive = false
}
next()
}
到此這篇關(guān)于Vue keep-alive組件的使用以及清除緩存的方法的文章就介紹到這了,更多相關(guān)Vue keep-alive組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue中Keep-Alive緩存組件使用語法及原理深度解析
- Vue3除了keep-alive還有哪些實(shí)現(xiàn)頁面緩存詳解
- React實(shí)現(xiàn)頁面狀態(tài)緩存(keep-alive)的示例代碼
- Vue路由組件的緩存keep-alive和include屬性的具體使用
- vue3?keep-alive實(shí)現(xiàn)tab頁面緩存功能
- Vue3嵌套路由中使用keep-alive緩存多層的實(shí)現(xiàn)
- vue使用keep-alive進(jìn)行組件緩存方法詳解(組件不緩存問題解決)
- vue中keep-alive組件實(shí)現(xiàn)多級(jí)嵌套路由的緩存
- 快速解決 keep-alive 緩存組件中定時(shí)器干擾問題
相關(guān)文章
Vue Autocomplete 自動(dòng)完成功能簡單示例
這篇文章主要介紹了Vue Autocomplete 自動(dòng)完成功能,結(jié)合簡單示例形式分析了Vue使用el-autocomplete組件實(shí)現(xiàn)自動(dòng)完成功能相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
vue?當(dāng)中組件之間共享數(shù)據(jù)的實(shí)現(xiàn)方式
這篇文章主要介紹了vue?當(dāng)中組件之間共享數(shù)據(jù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
關(guān)于vue-cli 3配置打包優(yōu)化要點(diǎn)(推薦)
這篇文章主要介紹了vue-cli 3配置打包優(yōu)化要點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue-calendar-component 封裝多日期選擇組件的實(shí)例代碼
這篇文章主要介紹了vue-calendar-component 封裝多日期選擇組件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
vue項(xiàng)目中使用lib-flexible解決移動(dòng)端適配的問題解決
這篇文章主要介紹了vue項(xiàng)目中使用lib-flexible解決移動(dòng)端適配的問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
vue2.0實(shí)現(xiàn)移動(dòng)端的輸入框?qū)崟r(shí)檢索更新列表功能
最近小編在做vue2.0的項(xiàng)目,遇到移動(dòng)端實(shí)時(shí)檢索搜索更新列表的效果,下面腳本之家小編給大家?guī)砹藇ue2.0 移動(dòng)端的輸入框?qū)崟r(shí)檢索更新列表功能的實(shí)例代碼,感興趣的朋友參考下吧2018-05-05

