有时候不一定需要exclude去控制哪些组件要被排除在缓存外面的,有些可能是根据实际情况决定是否控制缓存,这时候网上有
<keep-alive> <router-view :key="checkKeepAlive()"></router-view> </keep-alive>
JS:
checkKeepAlive() { if (!this.$route.meta.keepAlive) { console.log(‘输出‘,this.$route.name) return this.$route.name+Math.random(); } }
router:(我这里是自路由控制)
children: [ { path: "/home/test1", name: "test1", meta: { keepAlive: true }, component: () => import("@/components/Test1.vue") }, { path: "/home/test2", name: "test2", meta: { keepAlive: false }, component: () => import("@/components/Test2.vue") } ]
然后你就可以写JS去控制keepalive的true/false去实现缓存可控了
原文:https://www.cnblogs.com/llcdbk/p/12975396.html