fix: the cache problem in tabs mode;🐛

修复:多页签模式下的缓存问题;
This commit is contained in:
iczer
2020-08-07 22:14:56 +08:00
parent 9445f0fbbb
commit d741904f50
6 changed files with 199 additions and 57 deletions

View File

@@ -1,9 +1,6 @@
<template>
<page-toggle-transition :disabled="animate.disabled" :animate="animate.name" :direction="animate.direction">
<keep-alive :exclude="dustbins" v-if="multiPage">
<router-view />
</keep-alive>
<router-view v-else />
<router-view />
</page-toggle-transition>
</template>
@@ -15,7 +12,7 @@ export default {
name: 'BlankView',
components: {PageToggleTransition},
computed: {
...mapState('setting', ['multiPage', 'animate', 'dustbins'])
...mapState('setting', ['multiPage', 'animate'])
}
}
</script>

View File

@@ -4,10 +4,7 @@
<img :src="extraImage"/>
</div>
<page-toggle-transition :disabled="animate.disabled" :animate="animate.name" :direction="animate.direction">
<keep-alive :exclude="dustbins" v-if="multiPage">
<router-view ref="page" />
</keep-alive>
<router-view ref="page" v-else />
</page-toggle-transition>
</page-layout>
</template>
@@ -26,7 +23,7 @@ export default {
}
},
computed: {
...mapState('setting', ['isMobile', 'multiPage', 'animate', 'dustbins']),
...mapState('setting', ['isMobile', 'multiPage', 'animate']),
desc() {
return this.page.desc
},

View File

@@ -17,9 +17,9 @@
</a-tabs>
<div class="tabs-view-content" :style="`margin-top: ${multiPage ? -24 : 0}px`">
<page-toggle-transition :disabled="animate.disabled" :animate="animate.name" :direction="animate.direction">
<keep-alive :exclude="dustbins" v-if="multiPage">
<router-view :key="$route.fullPath" />
</keep-alive>
<a-keep-alive v-if="multiPage" v-model="clearCaches">
<router-view ref="tabContent" :key="$route.fullPath" />
</a-keep-alive>
<router-view v-else />
</page-toggle-transition>
</div>
@@ -32,20 +32,23 @@ import Contextmenu from '@/components/menu/Contextmenu'
import PageToggleTransition from '@/components/transition/PageToggleTransition'
import {mapState, mapMutations} from 'vuex'
import {getI18nKey} from '@/utils/routerUtil'
import AKeepAlive from '@/components/cache/AKeepAlive'
export default {
name: 'TabsView',
i18n: require('./i18n'),
components: { PageToggleTransition, Contextmenu, AdminLayout },
components: { PageToggleTransition, Contextmenu, AdminLayout , AKeepAlive },
data () {
return {
clearCaches: [],
pageList: [],
cachedKeys: [],
activePage: '',
menuVisible: false
}
},
computed: {
...mapState('setting', ['multiPage', 'animate', 'layout', 'dustbins']),
...mapState('setting', ['multiPage', 'animate', 'layout']),
menuItemList() {
return [
{ key: '1', icon: 'vertical-right', text: this.$t('closeLeft') },
@@ -67,6 +70,7 @@ export default {
},
mounted () {
this.correctPageMinHeight(-this.tabsOffset)
this.cachedKeys.push(this.$refs.tabContent.$vnode.key)
},
beforeDestroy() {
window.removeEventListener('page:close', this.closePageListener)
@@ -75,10 +79,12 @@ export default {
watch: {
'$route': function (newRoute) {
this.activePage = newRoute.fullPath
this.putCache(newRoute)
if (!this.multiPage) {
this.pageList = [newRoute]
} else if (this.pageList.findIndex(item => item.fullPath == newRoute.fullPath) == -1) {
this.$nextTick(() => {
this.cachedKeys.push(this.$refs.tabContent.$vnode.key)
})
this.pageList.push(newRoute)
}
},
@@ -107,9 +113,9 @@ export default {
return this.$message.warning(this.$t('warn'))
}
let index = this.pageList.findIndex(item => item.fullPath === key)
let pageRoute = this.pageList[index]
this.clearCache(pageRoute)
this.pageList = this.pageList.filter(item => item.fullPath !== key)
//清除缓存
this.clearCaches = this.cachedKeys.splice(index, 1)
this.pageList.splice(index, 1)
if (next) {
this.$router.push(next)
} else if (key === this.activePage) {
@@ -136,56 +142,40 @@ export default {
},
closeOthers (pageKey) {
const index = this.pageList.findIndex(item => item.fullPath === pageKey)
// 要关闭的页面清除缓存
this.pageList.forEach(item => {
if (item.fullPath !== pageKey){
this.clearCache(item)
}
})
// 清除缓存
this.clearCaches = this.cachedKeys.filter((item, i) => i != index)
this.cachedKeys = this.cachedKeys.slice(index, index + 1)
this.pageList = this.pageList.slice(index, index + 1)
this.activePage = this.pageList[0].fullPath
this.$router.push(this.activePage)
if (this.activePage != pageKey) {
this.activePage = pageKey
this.$router.push(this.activePage)
}
},
closeLeft (pageKey) {
const index = this.pageList.findIndex(item => item.fullPath === pageKey)
// 清除缓存
this.pageList.forEach((item, i) => {
if (i < index) {
this.clearCache(item)
}
})
this.clearCaches = this.cachedKeys.filter((item, i) => i < index)
this.cachedKeys = this.cachedKeys.slice(index)
this.pageList = this.pageList.slice(index)
if (this.pageList.findIndex(item => item.fullPath === this.activePage) === -1) {
this.activePage = this.pageList[0].fullPath
if (!this.pageList.find(item => item.fullPath === this.activePage)) {
this.activePage = pageKey
this.$router.push(this.activePage)
}
},
closeRight (pageKey) {
const index = this.pageList.findIndex(item => item.fullPath === pageKey)
// 清除缓存
this.pageList.forEach((item, i) => {
if (i > index) {
this.clearCache(item)
}
})
this.clearCaches = this.cachedKeys.filter((item, i) => i > index)
this.cachedKeys = this.cachedKeys.slice(0, index+1)
this.pageList = this.pageList.slice(0, index + 1)
if (this.pageList.findIndex(item => item.fullPath === this.activePage) === -1) {
this.activePage = this.pageList[this.pageList.length - 1].fullPath
if (!this.pageList.find(item => item.fullPath === this.activePage)) {
this.activePage = pageKey
this.$router.push(this.activePage)
}
},
clearCache(route) {
const componentName = route.matched.slice(-1)[0].components.default.name
if (this.dustbins.findIndex(item => item === componentName) === -1) {
this.setDustbins(this.dustbins.concat(componentName))
}
},
putCache(route) {
const componentName = route.matched.slice(-1)[0].components.default.name
if (this.dustbins.includes(componentName)) {
this.setDustbins(this.dustbins.filter(item => item !== componentName))
}
},
pageName(page) {
return this.$t(getI18nKey(page.matched[page.matched.length - 1].path))
},
@@ -194,7 +184,7 @@ export default {
const closePath = typeof closeRoute === 'string' ? closeRoute : closeRoute.path
this.remove(closePath, nextRoute)
},
...mapMutations('setting', ['setDustbins', 'correctPageMinHeight'])
...mapMutations('setting', ['correctPageMinHeight'])
}
}
/**