KeepRouterViewAlive小改

master
toesbieya 6 years ago
parent a274098763
commit 08c9a224b7
  1. 14
      vue/src/layout/component/Main/component/KeepRouterViewAlive.vue

@ -17,11 +17,9 @@ function getComponentName(opts) {
return opts && (opts.Ctor.options.name || opts.tag) return opts && (opts.Ctor.options.name || opts.tag)
} }
function pruneCacheEntry(cache, key, current) { function removeCache(cache, key) {
const cached = cache[key] const cached = cache[key]
if (cached && (!current || cached.tag !== current.tag)) { cached && cached.componentInstance && cached.componentInstance.$destroy()
cached.componentInstance && cached.componentInstance.$destroy()
}
delete cache[key] delete cache[key]
} }
@ -47,12 +45,12 @@ export default {
include: { include: {
deep: true, deep: true,
handler(val) { handler(val) {
const {cache, $route, _vnode} = this const {cache, $route} = this
for (const key of Object.keys(cache)) { for (const key of Object.keys(cache)) {
const cachedNode = cache[key] const cachedNode = cache[key]
const name = getCacheKey($route, cachedNode.componentOptions) const name = getCacheKey($route, cachedNode.componentOptions)
if (name && !val.includes(name)) { if (!val || !val.includes(name)) {
pruneCacheEntry(cache, key, _vnode) removeCache(cache, key)
} }
} }
} }
@ -65,7 +63,7 @@ export default {
beforeDestroy() { beforeDestroy() {
for (const key of Object.keys(this.cache)) { for (const key of Object.keys(this.cache)) {
pruneCacheEntry(this.cache, key) removeCache(this.cache, key)
} }
}, },

Loading…
Cancel
Save