mirror of
				https://github.com/soybeanjs/soybean-admin.git
				synced 2025-11-04 15:53:43 +08:00 
			
		
		
		
	refactor(projects): add reCacheRoute method
This commit is contained in:
		@@ -6,28 +6,21 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
<script setup lang="ts">
 | 
					<script setup lang="ts">
 | 
				
			||||||
import { useRoute } from 'vue-router';
 | 
					import { useRoute } from 'vue-router';
 | 
				
			||||||
import { useAppStore, useRouteStore } from '@/store';
 | 
					import { useRouteStore } from '@/store';
 | 
				
			||||||
import { useLoading } from '@/hooks';
 | 
					import { useLoading } from '@/hooks';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
defineOptions({ name: 'ReloadButton' });
 | 
					defineOptions({ name: 'ReloadButton' });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const app = useAppStore();
 | 
					const { reCacheRoute } = useRouteStore();
 | 
				
			||||||
const routeStore = useRouteStore();
 | 
					 | 
				
			||||||
const route = useRoute();
 | 
					const route = useRoute();
 | 
				
			||||||
const { loading, startLoading, endLoading } = useLoading();
 | 
					const { loading, startLoading, endLoading } = useLoading();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function handleRefresh() {
 | 
					async function handleRefresh() {
 | 
				
			||||||
  const isCached = routeStore.cacheRoutes.includes(String(route.name));
 | 
					 | 
				
			||||||
  if (isCached) {
 | 
					 | 
				
			||||||
    routeStore.removeCacheRoute(route.name as AuthRoute.AllRouteKey);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  startLoading();
 | 
					  startLoading();
 | 
				
			||||||
  app.reloadPage();
 | 
					
 | 
				
			||||||
  setTimeout(() => {
 | 
					  await reCacheRoute(route.name as AuthRoute.AllRouteKey);
 | 
				
			||||||
    if (isCached) {
 | 
					
 | 
				
			||||||
      routeStore.addCacheRoute(route.name as AuthRoute.AllRouteKey);
 | 
					  endLoading();
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    endLoading();
 | 
					 | 
				
			||||||
  }, 1000);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,6 +14,7 @@ import {
 | 
				
			|||||||
  transformRoutePathToRouteName,
 | 
					  transformRoutePathToRouteName,
 | 
				
			||||||
  sortRoutes
 | 
					  sortRoutes
 | 
				
			||||||
} from '@/utils';
 | 
					} from '@/utils';
 | 
				
			||||||
 | 
					import { useAppStore } from '../app';
 | 
				
			||||||
import { useAuthStore } from '../auth';
 | 
					import { useAuthStore } from '../auth';
 | 
				
			||||||
import { useTabStore } from '../tab';
 | 
					import { useTabStore } from '../tab';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -151,7 +152,6 @@ export const useRouteStore = defineStore('route-store', {
 | 
				
			|||||||
        await this.initStaticRoute();
 | 
					        await this.initStaticRoute();
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					 | 
				
			||||||
    /** 从缓存路由中去除某个路由 */
 | 
					    /** 从缓存路由中去除某个路由 */
 | 
				
			||||||
    removeCacheRoute(name: AuthRoute.AllRouteKey) {
 | 
					    removeCacheRoute(name: AuthRoute.AllRouteKey) {
 | 
				
			||||||
      const index = this.cacheRoutes.indexOf(name);
 | 
					      const index = this.cacheRoutes.indexOf(name);
 | 
				
			||||||
@@ -159,13 +159,30 @@ export const useRouteStore = defineStore('route-store', {
 | 
				
			|||||||
        this.cacheRoutes.splice(index, 1);
 | 
					        this.cacheRoutes.splice(index, 1);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					 | 
				
			||||||
    /** 添加某个缓存路由 */
 | 
					    /** 添加某个缓存路由 */
 | 
				
			||||||
    addCacheRoute(name: AuthRoute.AllRouteKey) {
 | 
					    addCacheRoute(name: AuthRoute.AllRouteKey) {
 | 
				
			||||||
      const index = this.cacheRoutes.indexOf(name);
 | 
					      const index = this.cacheRoutes.indexOf(name);
 | 
				
			||||||
      if (index === -1) {
 | 
					      if (index === -1) {
 | 
				
			||||||
        this.cacheRoutes.push(name);
 | 
					        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);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,6 @@
 | 
				
			|||||||
import { nextTick } from 'vue';
 | 
					 | 
				
			||||||
import type { RouteLocationNormalizedLoaded, Router } from 'vue-router';
 | 
					import type { RouteLocationNormalizedLoaded, Router } from 'vue-router';
 | 
				
			||||||
import { defineStore } from 'pinia';
 | 
					import { defineStore } from 'pinia';
 | 
				
			||||||
import { useRouteStore, useAppStore } from '@/store';
 | 
					import { useRouteStore } from '@/store';
 | 
				
			||||||
import { useRouterPush } from '@/composables';
 | 
					import { useRouterPush } from '@/composables';
 | 
				
			||||||
import { localStg } from '@/utils';
 | 
					import { localStg } from '@/utils';
 | 
				
			||||||
import { useThemeStore } from '../theme';
 | 
					import { useThemeStore } from '../theme';
 | 
				
			||||||
@@ -121,21 +120,12 @@ export const useTabStore = defineStore('tab-store', {
 | 
				
			|||||||
     * @param fullPath - 路由fullPath
 | 
					     * @param fullPath - 路由fullPath
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    async removeTab(fullPath: string) {
 | 
					    async removeTab(fullPath: string) {
 | 
				
			||||||
 | 
					      const { reCacheRoute } = useRouteStore();
 | 
				
			||||||
      const { routerPush } = useRouterPush(false);
 | 
					      const { routerPush } = useRouterPush(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // 清除keepAlive缓存
 | 
					      const tabName = this.tabs.find(tab => tab.fullPath === fullPath)?.name as AuthRoute.AllRouteKey | undefined;
 | 
				
			||||||
      const closeTabIndex = this.tabs.findIndex(tab => tab.fullPath === fullPath);
 | 
					      if (tabName) {
 | 
				
			||||||
      if (closeTabIndex !== -1) {
 | 
					        await reCacheRoute(tabName);
 | 
				
			||||||
        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 isActive = this.activeTab === fullPath;
 | 
					      const isActive = this.activeTab === fullPath;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,7 +8,16 @@
 | 
				
			|||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script setup lang="ts">
 | 
					<script setup lang="ts">
 | 
				
			||||||
 | 
					import { onActivated, onMounted } from 'vue';
 | 
				
			||||||
import { DevDependency, ProDependency, ProjectInfo, ProjectIntroduction } from './components';
 | 
					import { DevDependency, ProDependency, ProjectInfo, ProjectIntroduction } from './components';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					onActivated(() => {
 | 
				
			||||||
 | 
					  console.log('about page activated');
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					onMounted(() => {
 | 
				
			||||||
 | 
					  console.log('about page mounted');
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<style scoped></style>
 | 
					<style scoped></style>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user