问题遇到的现象和发生背景
A页面,B页面通过路由管理打开了,做切换动作,这时候B页面被KeepAlive缓存了,来回切换,B页面缓存很成功,A页面未做缓存操作,这时候A页面中有个按钮(带着条件),点击跳转B页面,需要清除B页面的缓存,然后A,B页面切换,B页面是缓存的最新的
运行结果
从一个非缓存界面跳转到一个缓存界面,在组件路由设置keepalive为false,离开这个页签后,再返回这个页签,是上上次打开的缓存
我想要达到的结果
可以直接用首页按钮打开界面的缓存覆盖第一次缓存,或者第一次缓存在点击首页按钮的时候清除
** 问题相关代码**
import Vue from 'vue'
import {setStore, getStore} from '@/utils/storage'
import store from './store';
Vue.mixin({
beforeRouteEnter: function (to, from, next) {
if(!to.path.includes('detail')&&to.path!='/normal/occupation'&&to.path!='/normal/ltc'&&to.path!='/authority/frontPageMgmt'
&&!to.path.includes('wel')
&&to.path!='/saber/payment'
&&to.path!='/saber/submitVoucher'
&&to.path!='/saber/orderDetail'
&&to.path!='/system/apvl-proc-config/index'
&& to.path != '/normal/flow/step'
) {
to.meta.keepAlive = true
}
if(to.params&&to.params.from&&(to.params.from=='first'||to.params.from=='fealther')) {
to.meta.keepAlive = false
}
next(() => {
let avueView = document.getElementById('avue-view');
if (avueView && to.meta.savedPosition) {
avueView.scrollTop = to.meta.savedPosition
}
})
},
beforeRouteLeave: function (to, from, next) {
console.log(this)
let avueView = document.getElementById('avue-view');
if (from && from.meta.keepAlive) {
if (avueView) {
from.meta.savedPosition = avueView.scrollTop
}
const result = this.$route.meta.keepAlive === true && store.state.tags.tagList.some(ele => {
return ele.value === this.$route.fullPath;
});
if (this.$vnode && !result) {
from.meta.savedPosition = 0
if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) {
if (this.$vnode.componentOptions) {
let key = this.$vnode.key == null
? this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '')
: this.$vnode.key;
let cache = this.$vnode.parent.componentInstance.cache;
let keys = this.$vnode.parent.componentInstance.keys;
if (cache[key]) {
if (keys.length) {
let index = keys.indexOf(key);
if (index > -1) {
keys.splice(index, 1);
}
}
delete cache[key];
}
}
}
}
}
//按钮清缓存
if(getStore({name: 'clearTag'})){
if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) {
if (this.$vnode.componentOptions) {
let key = this.$vnode.key == null
? this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '')
: this.$vnode.key;
let cache = this.$vnode.parent.componentInstance.cache;
let keys = this.$vnode.parent.componentInstance.keys;
if(keys.length>0){
//删除所有,清除缓存
if(getStore({name: 'clearTag'})=='all'||getStore({name: 'clearTag'})=='cache'){
Object.keys(cache).forEach((item)=>{
console.log("item",item,keys,key)
if(cache[item]){
delete cache[item];
}
})
this.$vnode.parent.componentInstance.cache = {};
this.$vnode.parent.componentInstance.keys = []
}else{
//关闭其他
Object.keys(cache).forEach((item,i)=>{
if(cache[item]&&key!=item){
delete cache[item];
keys.splice(i, 1);
}
})
}
}
}
}
setStore({name: 'clearTag', content: ''})
}
next();
},
});