Vue keepalive缓存清理思路

Posted Comedy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vue keepalive缓存清理思路相关的知识,希望对你有一定的参考价值。

涉及知识点:vuex,mixins,keepalive,route,computed,watch

 

1./store/index.js  :

   store:  
    keepAliveList:[],
mutations:   setKeepAliveLists(state,keepName){       state.keepAliveList.push(keepName);     },     removeKeepAliveItem(state,keepName){       state.keepAliveList.splice(state.keepAliveList.indexOf(keepName),
1)     },

 

2./components/TopWatch :(非左侧标签导航组件,点击路由切换,可关闭)

    监听路由变化,当其meta.keepAlive为true时将该路由的name存入keepAliveList
    this.$store.commit(\'setKeepAliveLists\',storeCache);
点击关闭标签时,当其meta.keepAlive为true时将该路由的name存入keepAliveList
this.$store.commit(\'removeKeepAliveItem\',storeCache);

 

3.引入mixins在需要动态销毁的组件里,监听store.state.keepAliveList

import mixin from \'../../mixin\'

export default {
    mixins:[mixin],
}

 

4.当mixins所在组件的name不存在于store.state.keepAliveList时,执行this.$destroy()

export default {
    computed: {
        keepAliveConf(){
            return this.$store.state.keepAliveList;
        }
    },
    watch:{
        keepAliveConf(e){
            // 监听缓存列表的变化,如果缓存列表中没有当前的路由或组件则在缓存中销毁该实例
            let name = this.$options.name;
            if(!this.$store.state.keepAliveList.includes(name)) {
                this.$destroy()
            }
        }
    },
}

 

以上是关于Vue keepalive缓存清理思路的主要内容,如果未能解决你的问题,请参考以下文章

vue项目微信端清理缓存问题解决

vue中手动清除KeepAlive缓存

利用Vue中keep-alive,快速实现页面缓存

解决vue keepAlive 二次进入页面显示首次缓存问题

keepAlive动态缓存及清除缓存

Vue之keep-alive灵活清除页面缓存