Template
1
0
mirror of https://github.com/un-pany/v3-admin-vite.git synced 2025-04-22 03:49:19 +08:00

fix: keepalive invalid

This commit is contained in:
QC2168 2024-10-08 17:26:05 +08:00
parent cd3fcd3b1f
commit 224712f9ba
2 changed files with 27 additions and 4 deletions

View File

@ -1,9 +1,9 @@
<script lang="ts" setup>
import { useTagsViewStore } from "@/store/modules/tags-view"
import { useSettingsStore } from "@/store/modules/settings"
import Footer from "./Footer/index.vue"
const tagsViewStore = useTagsViewStore()
import router from "@/router/index"
import { filterKeepAlive } from "@/router/helper"
const keepAliveInclude = filterKeepAlive(router)
const settingsStore = useSettingsStore()
</script>
@ -13,7 +13,7 @@ const settingsStore = useSettingsStore()
<!-- key 采用 route.path route.fullPath 有着不同的效果大多数时候 path 更通用 -->
<router-view v-slot="{ Component, route }">
<transition name="el-fade-in" mode="out-in">
<keep-alive :include="tagsViewStore.cachedViews">
<keep-alive :include="keepAliveInclude">
<component :is="Component" :key="route.path" class="app-container-grow" />
</keep-alive>
</transition>

View File

@ -67,3 +67,26 @@ const addToChildren = (routes: RouteRecordNormalized[], children: RouteRecordRaw
}
})
}
/* 过滤需要keepalive的标签 */
export const filterKeepAlive = (router: Router): string[] => {
const keepAliveList: string[] = []
const routes = router.getRoutes() || []
const filter = (route: RouteRecordRaw[] | RouteRecordRaw) => {
// 确保 route 是数组
const routes = Array.isArray(route) ? route : [route]
routes.forEach((item: RouteRecordRaw) => {
if (item.meta?.keepAlive === true) {
keepAliveList.push(item.name as string)
}
if (item.children?.length) {
filter(item.children) // 递归处理子路由
}
})
}
filter(routes) // 初始调用
return keepAliveList
}