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)容請搜索腳本之家以前的文章或繼續(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)多級嵌套路由的緩存
- 快速解決 keep-alive 緩存組件中定時(shí)器干擾問題
相關(guān)文章
使用vue制作探探滑動(dòng)堆疊組件的實(shí)例代碼
探探的堆疊滑動(dòng)組件起到了關(guān)鍵的作用,下面就來看看如何用vue寫一個(gè)探探的堆疊組件,感興趣的朋友一起看看吧2018-03-03在VUE中實(shí)現(xiàn)文件下載并判斷狀態(tài)的方法
今天小編就為大家分享一篇在VUE中實(shí)現(xiàn)文件下載并判斷狀態(tài)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue列表數(shù)據(jù)刪除后主動(dòng)刷新頁面及刷新方法詳解
這篇文章主要給大家介紹了關(guān)于vue列表數(shù)據(jù)刪除后主動(dòng)刷新頁面及刷新方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Vue安裝依賴npm install時(shí)的報(bào)錯(cuò)問題及解決
這篇文章主要介紹了Vue安裝依賴npm install時(shí)的報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Vue 框架之動(dòng)態(tài)綁定 css 樣式實(shí)例分析
這篇文章主要介紹了Vue 框架之動(dòng)態(tài)綁定 css 樣式的方法,本文通過分享小實(shí)例給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11