Browse Source

perf: 优化路由守卫

xiaoxian521 2 years ago
parent
commit
b606fa454d
1 changed files with 6 additions and 2 deletions
  1. 6 2
      src/router/index.ts

+ 6 - 2
src/router/index.ts

@@ -134,6 +134,10 @@ router.beforeEach((to: toRouteType, _from, next) => {
       else document.title = transformI18n(item.meta.title);
     });
   }
+  /** 如果已经登录并存在登录信息后不能跳转到路由白名单,而是继续保持在当前页面 */
+  function toCorrectRoute() {
+    whiteList.includes(to.fullPath) ? next(_from.fullPath) : next();
+  }
   if (userInfo) {
     // 无权限跳转403页面
     if (to.meta?.roles && !isOneOfArray(to.meta?.roles, userInfo?.roles)) {
@@ -145,7 +149,7 @@ router.beforeEach((to: toRouteType, _from, next) => {
         openLink(to?.name as string);
         NProgress.done();
       } else {
-        next();
+        toCorrectRoute();
       }
     } else {
       // 刷新
@@ -175,7 +179,7 @@ router.beforeEach((to: toRouteType, _from, next) => {
           }
           router.push(to.fullPath);
         });
-      next();
+      toCorrectRoute();
     }
   } else {
     if (to.path !== "/login") {