路由缓存修改

This commit is contained in:
张巍 2020-08-05 09:02:12 +08:00
parent 5e7113935c
commit 8157327eaf
3 changed files with 110 additions and 12 deletions

View File

@ -0,0 +1,94 @@
<script>
/**URL
* 日期:2020/8/4
*/
var cache = Object.create(null);
var keys = [];
function remove(keys, key) {
if (keys && key) {
var index = keys.indexOf(key);
if (index != -1) {
keys.splice(index, 1);
}
}
}
/**
* 修剪缓存
*/
function pruneCache(keepAliveInstance, filter) {
const { cache, keys, _vnode } = keepAliveInstance;
for (const key in cache) {
const cachedNode = cache[key];
if (cachedNode) {
if (!filter(key)) {
pruneCacheEntry(cache, key, keys, _vnode);
}
}
}
console.log("页面缓存数量:" + keys.length);
}
/**
* 修剪缓存入口
*/
function pruneCacheEntry(cache, key, keys, current) {
const cached = cache[key];
// destorydestroykey-value
cached && cached.componentInstance.$destroy();
cache[key] = null;
remove(keys, key);
}
export default {
name: "v-keep-alive",
//
abstract: true,
props: {
keyArray: []
},
created() {
//
this.cache = cache;
this.keys = keys;
},
destroyed() {
//
// for (const key in this.cache) {
// pruneCacheEntry(this.cache, key, this.keys);
// }
},
mounted() {
this.$watch("keyArray", val => {
//key
pruneCache(this, name => val.some(v => v == name));
});
},
render() {
const slot = this.$slots.default;
const vnode = slot[0];
const componentOptions = vnode && vnode.componentOptions;
this.$vnode.componentOptions.Ctor.options.abstract = true;
if (componentOptions) {
const { cache, keys } = this;
const key =
vnode.key.indexOf("__transition") == 0 ? vnode.data.key : vnode.key;
if (cache[key]) {
vnode.componentInstance = cache[key].componentInstance;
remove(keys, key);
keys.push(key);
console.log("页面缓存数量:" + keys.length);
} else {
cache[key] = vnode;
keys.push(key);
console.log("页面缓存数量:" + keys.length);
}
vnode.data.keepAlive = true;
}
return vnode || (slot && slot[0]);
}
};
</script>

View File

@ -1,25 +1,29 @@
<template>
<section class="app-main">
<transition name="fade-transform" mode="out-in">
<keep-alive :include="cachedViews">
<v-keep-alive :keyArray="cachedViews">
<router-view :key="key" />
</keep-alive>
</v-keep-alive>
</transition>
</section>
</template>
<script>
import vKeepAlive from "../../components/V/v-keep-alive";
export default {
name: 'AppMain',
name: "AppMain",
components: {
vKeepAlive
},
computed: {
cachedViews() {
return this.$store.state.tagsView.cachedViews
return this.$store.state.tagsView.cachedViews;
},
key() {
return this.$route.path
return this.$route.path;
}
}
}
};
</script>
<style lang="scss" scoped>
@ -31,7 +35,7 @@ export default {
overflow: hidden;
}
.fixed-header+.app-main {
.fixed-header + .app-main {
padding-top: 50px;
}
@ -41,7 +45,7 @@ export default {
min-height: calc(100vh - 84px);
}
.fixed-header+.app-main {
.fixed-header + .app-main {
padding-top: 84px;
}
}

View File

@ -13,9 +13,9 @@ const mutations = {
)
},
ADD_CACHED_VIEW: (state, view) => {
if (state.cachedViews.includes(view.name)) return
if (state.cachedViews.includes(view.path)) return
if (!view.meta.noCache) {
state.cachedViews.push(view.name)
state.cachedViews.push(view.path)
}
},
@ -28,7 +28,7 @@ const mutations = {
}
},
DEL_CACHED_VIEW: (state, view) => {
const index = state.cachedViews.indexOf(view.name)
const index = state.cachedViews.indexOf(view.path)
index > -1 && state.cachedViews.splice(index, 1)
},
@ -38,7 +38,7 @@ const mutations = {
})
},
DEL_OTHERS_CACHED_VIEWS: (state, view) => {
const index = state.cachedViews.indexOf(view.name)
const index = state.cachedViews.indexOf(view.path)
if (index > -1) {
state.cachedViews = state.cachedViews.slice(index, index + 1)
} else {