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三個組件需要在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 值設置為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 值設置為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()
}
到此這篇關于Vue keep-alive組件的使用以及清除緩存的方法的文章就介紹到這了,更多相關Vue keep-alive組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Vue中Keep-Alive緩存組件使用語法及原理深度解析
- Vue3除了keep-alive還有哪些實現(xiàn)頁面緩存詳解
- React實現(xiàn)頁面狀態(tài)緩存(keep-alive)的示例代碼
- Vue路由組件的緩存keep-alive和include屬性的具體使用
- vue3?keep-alive實現(xiàn)tab頁面緩存功能
- Vue3嵌套路由中使用keep-alive緩存多層的實現(xiàn)
- vue使用keep-alive進行組件緩存方法詳解(組件不緩存問題解決)
- vue中keep-alive組件實現(xiàn)多級嵌套路由的緩存
- 快速解決 keep-alive 緩存組件中定時器干擾問題
相關文章
在VUE中實現(xiàn)文件下載并判斷狀態(tài)的方法
今天小編就為大家分享一篇在VUE中實現(xiàn)文件下載并判斷狀態(tài)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue列表數(shù)據(jù)刪除后主動刷新頁面及刷新方法詳解
這篇文章主要給大家介紹了關于vue列表數(shù)據(jù)刪除后主動刷新頁面及刷新方法的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05

