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' ;
82import type { RouteKey , RoutePath } from '@elegant-router/types' ;
93import { useAuthStore } from '@/store/modules/auth' ;
104import { useRouteStore } from '@/store/modules/route' ;
@@ -17,12 +11,11 @@ import { getRouteName } from '@/router/elegant/transform';
1711 * @param router router instance
1812 */
1913export 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
177163function getRouteQueryOfLoginRoute ( to : RouteLocationNormalized , routeHome : RouteKey ) {
0 commit comments