From f92ee770e056ab8a9a264d8a43fcf4893f62c7fa Mon Sep 17 00:00:00 2001 From: Soybean Date: Wed, 6 Sep 2023 00:32:43 +0800 Subject: [PATCH] refactor(projects): add reCacheRoute method --- .../components/reload-button/index.vue | 23 +++++++------------ src/store/modules/route/index.ts | 21 +++++++++++++++-- src/store/modules/tab/index.ts | 20 ++++------------ src/views/about/index.vue | 9 ++++++++ 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/layouts/common/global-tab/components/reload-button/index.vue b/src/layouts/common/global-tab/components/reload-button/index.vue index d1468a0f..253ca1df 100644 --- a/src/layouts/common/global-tab/components/reload-button/index.vue +++ b/src/layouts/common/global-tab/components/reload-button/index.vue @@ -6,28 +6,21 @@ diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index 80db75f5..2b5bde66 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -14,6 +14,7 @@ import { transformRoutePathToRouteName, sortRoutes } from '@/utils'; +import { useAppStore } from '../app'; import { useAuthStore } from '../auth'; import { useTabStore } from '../tab'; @@ -151,7 +152,6 @@ export const useRouteStore = defineStore('route-store', { await this.initStaticRoute(); } }, - /** 从缓存路由中去除某个路由 */ removeCacheRoute(name: AuthRoute.AllRouteKey) { const index = this.cacheRoutes.indexOf(name); @@ -159,13 +159,30 @@ export const useRouteStore = defineStore('route-store', { this.cacheRoutes.splice(index, 1); } }, - /** 添加某个缓存路由 */ addCacheRoute(name: AuthRoute.AllRouteKey) { const index = this.cacheRoutes.indexOf(name); if (index === -1) { this.cacheRoutes.push(name); } + }, + /** + * 重新缓存路由 + */ + async reCacheRoute(name: AuthRoute.AllRouteKey) { + const { reloadPage } = useAppStore(); + + const isCached = this.cacheRoutes.includes(name); + + if (isCached) { + this.removeCacheRoute(name); + } + + await reloadPage(); + + if (isCached) { + this.addCacheRoute(name as AuthRoute.AllRouteKey); + } } } }); diff --git a/src/store/modules/tab/index.ts b/src/store/modules/tab/index.ts index 3676e420..e3471867 100644 --- a/src/store/modules/tab/index.ts +++ b/src/store/modules/tab/index.ts @@ -1,7 +1,6 @@ -import { nextTick } from 'vue'; import type { RouteLocationNormalizedLoaded, Router } from 'vue-router'; import { defineStore } from 'pinia'; -import { useRouteStore, useAppStore } from '@/store'; +import { useRouteStore } from '@/store'; import { useRouterPush } from '@/composables'; import { localStg } from '@/utils'; import { useThemeStore } from '../theme'; @@ -121,21 +120,12 @@ export const useTabStore = defineStore('tab-store', { * @param fullPath - 路由fullPath */ async removeTab(fullPath: string) { + const { reCacheRoute } = useRouteStore(); const { routerPush } = useRouterPush(false); - // 清除keepAlive缓存 - const closeTabIndex = this.tabs.findIndex(tab => tab.fullPath === fullPath); - if (closeTabIndex !== -1) { - const appStore = useAppStore(); - const routeStore = useRouteStore(); - const closeTabName = this.tabs[closeTabIndex].name as AuthRoute.AllRouteKey; - - routeStore.removeCacheRoute(closeTabName); - appStore.reloadPage(); - - nextTick(() => { - routeStore.addCacheRoute(closeTabName); - }); + const tabName = this.tabs.find(tab => tab.fullPath === fullPath)?.name as AuthRoute.AllRouteKey | undefined; + if (tabName) { + await reCacheRoute(tabName); } const isActive = this.activeTab === fullPath; diff --git a/src/views/about/index.vue b/src/views/about/index.vue index c174ddee..c89fffaa 100644 --- a/src/views/about/index.vue +++ b/src/views/about/index.vue @@ -8,7 +8,16 @@