fix: keepAlive

This commit is contained in:
张传龙
2023-07-11 15:07:37 +08:00
parent d49af8b574
commit d702a6703b
5 changed files with 33 additions and 48 deletions

View File

@@ -1,23 +1,16 @@
<template>
<router-view v-slot="{ Component, route }">
<KeepAlive :include="keepAliveRouteNames">
<component
:is="Component"
v-if="appStore.reloadFlag"
:key="appStore.aliveKeys[route.name] || route.fullPath"
/>
<KeepAlive :include="keepAliveNames">
<component :is="Component" v-if="!tagStore.reloading" :key="route.fullPath" />
</KeepAlive>
</router-view>
</template>
<script setup>
import { useAppStore } from '@/store'
import { useRouter } from 'vue-router'
const appStore = useAppStore()
const router = useRouter()
import { useTagsStore } from '@/store'
const tagStore = useTagsStore()
const allRoutes = router.getRoutes()
const keepAliveRouteNames = computed(() => {
return allRoutes.filter((route) => route.meta?.keepAlive).map((route) => route.name)
const keepAliveNames = computed(() => {
return tagStore.tags.filter((item) => item.keepAlive).map((item) => item.name)
})
</script>

View File

@@ -11,7 +11,7 @@
</template>
<script setup>
import { useTagsStore, useAppStore } from '@/store'
import { useTagsStore } from '@/store'
import { renderIcon } from '@/utils'
const props = defineProps({
@@ -36,7 +36,6 @@ const props = defineProps({
const emit = defineEmits(['update:show'])
const tagsStore = useTagsStore()
const appStore = useAppStore()
const options = computed(() => [
{
@@ -78,11 +77,7 @@ const actionMap = new Map([
[
'reload',
() => {
if (route.meta?.keepAlive) {
// 重置keepAlive
appStore.setAliveKeys(route.name, +new Date())
}
appStore.reloadPage()
tagsStore.reloadTag(route.path, route.meta?.keepAlive)
},
],
[

View File

@@ -50,7 +50,8 @@ watch(
const { name, fullPath: path } = route
const title = route.meta?.title
const icon = route.meta?.icon
tagsStore.addTag({ name, path, title, icon })
const keepAlive = route.meta?.keepAlive
tagsStore.addTag({ name, path, title, icon, keepAlive })
},
{ immediate: true }
)