fix(router): simplify route guard logic and remove unnecessary next calls

This commit is contained in:
soybeanfe
2026-03-09 12:42:50 +08:00
parent 6fc6f1c9b3
commit 3c2cbb7488
2 changed files with 13 additions and 27 deletions

View File

@@ -1,11 +1,11 @@
import type { Router } from 'vue-router'; import type { Router } from 'vue-router';
export function createProgressGuard(router: Router) { export function createProgressGuard(router: Router) {
router.beforeEach((_to, _from, next) => { router.beforeEach(() => {
window.NProgress?.start?.(); window.NProgress?.start?.();
next(); return;
}); });
router.afterEach(_to => { router.afterEach(() => {
window.NProgress?.done?.(); window.NProgress?.done?.();
}); });
} }

View File

@@ -1,10 +1,4 @@
import type { import type { LocationQueryRaw, RouteLocationNormalized, RouteLocationRaw, Router } from 'vue-router';
LocationQueryRaw,
NavigationGuardNext,
RouteLocationNormalized,
RouteLocationRaw,
Router
} from 'vue-router';
import type { RouteKey, RoutePath } from '@elegant-router/types'; import type { RouteKey, RoutePath } from '@elegant-router/types';
import { useAuthStore } from '@/store/modules/auth'; import { useAuthStore } from '@/store/modules/auth';
import { useRouteStore } from '@/store/modules/route'; import { useRouteStore } from '@/store/modules/route';
@@ -17,12 +11,11 @@ import { getRouteName } from '@/router/elegant/transform';
* @param router router instance * @param router router instance
*/ */
export function createRouteGuard(router: Router) { export function createRouteGuard(router: Router) {
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from) => {
const location = await initRoute(to); const location = await initRoute(to);
if (location) { if (location) {
next(location); return location;
return;
} }
const authStore = useAuthStore(); const authStore = useAuthStore();
@@ -40,30 +33,27 @@ export function createRouteGuard(router: Router) {
// if it is login route when logged in, then switch to the root page // if it is login route when logged in, then switch to the root page
if (to.name === loginRoute && isLogin) { if (to.name === loginRoute && isLogin) {
next({ name: rootRoute }); return { name: rootRoute };
return;
} }
// if the route does not need login, then it is allowed to access directly // if the route does not need login, then it is allowed to access directly
if (!needLogin) { if (!needLogin) {
handleRouteSwitch(to, from, next); handleRouteSwitch(to, from);
return; return;
} }
// the route need login but the user is not logged in, then switch to the login page // the route need login but the user is not logged in, then switch to the login page
if (!isLogin) { if (!isLogin) {
next({ name: loginRoute, query: { redirect: to.fullPath } }); return { name: loginRoute, query: { redirect: to.fullPath } };
return;
} }
// if the user is logged in but does not have authorization, then switch to the 403 page // if the user is logged in but does not have authorization, then switch to the 403 page
if (!hasAuth) { if (!hasAuth) {
next({ name: noAuthorizationRoute }); return { name: noAuthorizationRoute };
return;
} }
// switch route normally // switch route normally
handleRouteSwitch(to, from, next); handleRouteSwitch(to, from);
}); });
} }
@@ -161,17 +151,13 @@ async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw
return null; return null;
} }
function handleRouteSwitch(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) { function handleRouteSwitch(to: RouteLocationNormalized, from: RouteLocationNormalized) {
// route with href // route with href
if (to.meta.href) { if (to.meta.href) {
window.open(to.meta.href, '_blank'); window.open(to.meta.href, '_blank');
next({ path: from.fullPath, replace: true, query: from.query, hash: to.hash }); return { path: from.fullPath, replace: true, query: from.query, hash: to.hash };
return;
} }
next();
} }
function getRouteQueryOfLoginRoute(to: RouteLocationNormalized, routeHome: RouteKey) { function getRouteQueryOfLoginRoute(to: RouteLocationNormalized, routeHome: RouteKey) {