@@ -13,6 +13,7 @@ import {
1313 RouteLocationOptions ,
1414 MatcherLocationRaw ,
1515 RouteParams ,
16+ NavigationGuardReturn ,
1617} from './types'
1718import { RouterHistory , HistoryState , NavigationType } from './history/common'
1819import {
@@ -778,6 +779,14 @@ export function createRouter(options: RouterOptions): Router {
778779 return error ? Promise . reject ( error ) : Promise . resolve ( )
779780 }
780781
782+ function runWithContext < T > ( fn : ( ) => T ) : T {
783+ const app : App | undefined = installedApps . values ( ) . next ( ) . value
784+ // support Vue < 3.3
785+ return app && typeof app . runWithContext === 'function'
786+ ? app . runWithContext ( fn )
787+ : fn ( )
788+ }
789+
781790 // TODO: refactor the whole before guards by internally using router.beforeEach
782791
783792 function navigate (
@@ -907,7 +916,9 @@ export function createRouter(options: RouterOptions): Router {
907916 ) : void {
908917 // navigation is confirmed, call afterGuards
909918 // TODO: wrap with error handlers
910- for ( const guard of afterGuards . list ( ) ) guard ( to , from , failure )
919+ for ( const guard of afterGuards . list ( ) ) {
920+ runWithContext ( ( ) => guard ( to , from , failure ) )
921+ }
911922 }
912923
913924 /**
@@ -1263,14 +1274,15 @@ export function createRouter(options: RouterOptions): Router {
12631274 } ,
12641275 }
12651276
1266- return router
1267- }
1277+ // TODO: type this as NavigationGuardReturn or similar instead of any
1278+ function runGuardQueue ( guards : Lazy < any > [ ] ) : Promise < any > {
1279+ return guards . reduce (
1280+ ( promise , guard ) => promise . then ( ( ) => runWithContext ( guard ) ) ,
1281+ Promise . resolve ( )
1282+ )
1283+ }
12681284
1269- function runGuardQueue ( guards : Lazy < any > [ ] ) : Promise < void > {
1270- return guards . reduce (
1271- ( promise , guard ) => promise . then ( ( ) => guard ( ) ) ,
1272- Promise . resolve ( )
1273- )
1285+ return router
12741286}
12751287
12761288function extractChangingRecords (
0 commit comments