Skip to content

Commit 1f02b4e

Browse files
committed
fix(router): simplify route guard logic and remove unnecessary next calls
1 parent fe0d8dd commit 1f02b4e

2 files changed

Lines changed: 13 additions & 27 deletions

File tree

src/router/guard/progress.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { Router } from 'vue-router';
22

33
export function createProgressGuard(router: Router) {
4-
router.beforeEach((_to, _from, next) => {
4+
router.beforeEach(() => {
55
window.NProgress?.start?.();
6-
next();
6+
return;
77
});
8-
router.afterEach(_to => {
8+
router.afterEach(() => {
99
window.NProgress?.done?.();
1010
});
1111
}

src/router/guard/route.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import type {
2-
LocationQueryRaw,
3-
NavigationGuardNext,
4-
RouteLocationNormalized,
5-
RouteLocationRaw,
6-
Router
7-
} from 'vue-router';
1+
import type { LocationQueryRaw, RouteLocationNormalized, RouteLocationRaw, Router } from 'vue-router';
82
import type { RouteKey, RoutePath } from '@elegant-router/types';
93
import { useAuthStore } from '@/store/modules/auth';
104
import { useRouteStore } from '@/store/modules/route';
@@ -17,12 +11,11 @@ import { getRouteName } from '@/router/elegant/transform';
1711
* @param router router instance
1812
*/
1913
export function createRouteGuard(router: Router) {
20-
router.beforeEach(async (to, from, next) => {
14+
router.beforeEach(async (to, from) => {
2115
const location = await initRoute(to);
2216

2317
if (location) {
24-
next(location);
25-
return;
18+
return location;
2619
}
2720

2821
const authStore = useAuthStore();
@@ -40,30 +33,27 @@ export function createRouteGuard(router: Router) {
4033

4134
// if it is login route when logged in, then switch to the root page
4235
if (to.name === loginRoute && isLogin) {
43-
next({ name: rootRoute });
44-
return;
36+
return { name: rootRoute };
4537
}
4638

4739
// if the route does not need login, then it is allowed to access directly
4840
if (!needLogin) {
49-
handleRouteSwitch(to, from, next);
41+
handleRouteSwitch(to, from);
5042
return;
5143
}
5244

5345
// the route need login but the user is not logged in, then switch to the login page
5446
if (!isLogin) {
55-
next({ name: loginRoute, query: { redirect: to.fullPath } });
56-
return;
47+
return { name: loginRoute, query: { redirect: to.fullPath } };
5748
}
5849

5950
// if the user is logged in but does not have authorization, then switch to the 403 page
6051
if (!hasAuth) {
61-
next({ name: noAuthorizationRoute });
62-
return;
52+
return { name: noAuthorizationRoute };
6353
}
6454

6555
// switch route normally
66-
handleRouteSwitch(to, from, next);
56+
handleRouteSwitch(to, from);
6757
});
6858
}
6959

@@ -161,17 +151,13 @@ async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw
161151
return null;
162152
}
163153

164-
function handleRouteSwitch(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) {
154+
function handleRouteSwitch(to: RouteLocationNormalized, from: RouteLocationNormalized) {
165155
// route with href
166156
if (to.meta.href) {
167157
window.open(to.meta.href, '_blank');
168158

169-
next({ path: from.fullPath, replace: true, query: from.query, hash: to.hash });
170-
171-
return;
159+
return { path: from.fullPath, replace: true, query: from.query, hash: to.hash };
172160
}
173-
174-
next();
175161
}
176162

177163
function getRouteQueryOfLoginRoute(to: RouteLocationNormalized, routeHome: RouteKey) {

0 commit comments

Comments
 (0)