From 15aec11c78aedf482473a17867aaf261905afdf6 Mon Sep 17 00:00:00 2001 From: bqy_fe <1743369777@qq.com> Date: Thu, 30 Dec 2021 16:19:09 +0800 Subject: [PATCH] fix: :bug: router navigation bug --- package.json | 2 +- src/router/generator-router.tsx | 2 +- src/router/index.ts | 9 ++++----- src/router/router-guards.ts | 7 +++++-- src/views/shared/demos/tables/lol-table/heroInfo.vue | 6 ++++++ yarn.lock | 12 ++++++------ 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 4fd32d1b3..fe54b40d1 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,7 @@ "stylelint-order": "^5.0.0", "svg-sprite-loader": "^6.0.11", "typescript": "^4.5.4", - "vue-cli-plugin-windicss": "^1.1.2", + "vue-cli-plugin-windicss": "^1.1.3", "vue-eslint-parser": "^8.0.1" }, "__npminstall_done": false, diff --git a/src/router/generator-router.tsx b/src/router/generator-router.tsx index fb5ec39d9..33042a420 100644 --- a/src/router/generator-router.tsx +++ b/src/router/generator-router.tsx @@ -106,7 +106,6 @@ export const generatorDynamicRouter = (asyncMenus: API.Menu[]) => { const removeRoute = router.addRoute(layout); // 获取所有没有包含children的路由,上面addRoute的时候,vue-router已经帮我们拍平了所有路由 const filterRoutes = router.getRoutes().filter((item) => !item.children.length); - // console.log('所有路由', filterRoutes); // 清空所有路由 removeRoute(); layout.children = filterRoutes; @@ -114,6 +113,7 @@ export const generatorDynamicRouter = (asyncMenus: API.Menu[]) => { router.addRoute(layout); // 追加末尾路由 endRoutes.forEach((item) => router.addRoute(item)); + // console.log('所有路由', router.getRoutes()); return { menus: menus, routes: layout.children, diff --git a/src/router/index.ts b/src/router/index.ts index 5a3cf0fde..ecec7ffab 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -6,14 +6,13 @@ import { createRouterGuards } from './router-guards'; import common from '@/router/staticModules'; import shared from './staticModules/besidesLayout'; -import { notFound, errorRoutes } from './staticModules/error'; import { whiteNameList } from './constant'; export const routes: Array = [ { path: '/', name: 'Layout', - redirect: '/dashboard', + redirect: '/dashboard/welcome', component: () => import(/* webpackChunkName: "layout" */ '@/layout/index.vue'), meta: { title: '首页', @@ -21,8 +20,6 @@ export const routes: Array = [ children: [...common], }, ...shared, - notFound, - errorRoutes, ]; export const router = createRouter({ @@ -42,9 +39,11 @@ export function resetRouter() { } export async function setupRouter(app: App) { - app.use(router); // 创建路由守卫 createRouterGuards(router, whiteNameList); + + app.use(router); + // 路由准备就绪后挂载APP实例 await router.isReady(); } diff --git a/src/router/router-guards.ts b/src/router/router-guards.ts index 5adefc375..e1997f477 100644 --- a/src/router/router-guards.ts +++ b/src/router/router-guards.ts @@ -9,13 +9,14 @@ import { type WhiteNameList } from './constant'; NProgress.configure({ showSpinner: false }); // NProgress Configuration const loginRoutePath = '/login'; -const defaultRoutePath = '/dashboard'; +const defaultRoutePath = '/dashboard/welcome'; export function createRouterGuards(router: Router, whiteNameList: WhiteNameList) { router.beforeEach(async (to, from, next) => { const userStore = useUserStore(); NProgress.start(); // start progress bar const token = Storage.get(ACCESS_TOKEN_KEY); + if (token) { if (to.name === 'login') { next({ path: defaultRoutePath }); @@ -72,7 +73,9 @@ export function createRouterGuards(router: Router, whiteNameList: WhiteNameList) if (componentName) { keepAliveStore.add(componentName); } else { - console.warn(`${to.fullPath}页面组件未设置组件名,会导致缓存失效,请检查`); + console.warn( + `${to.fullPath}页面组件的keepAlive为true但未设置组件名,会导致缓存失效,请检查`, + ); } } else { // 不需要缓存的组件 diff --git a/src/views/shared/demos/tables/lol-table/heroInfo.vue b/src/views/shared/demos/tables/lol-table/heroInfo.vue index f594e98b5..8059f3e53 100644 --- a/src/views/shared/demos/tables/lol-table/heroInfo.vue +++ b/src/views/shared/demos/tables/lol-table/heroInfo.vue @@ -46,6 +46,12 @@ + +