diff --git a/packages/router/src/navigationGuards.ts b/packages/router/src/navigationGuards.ts index 148a51f27..35642cc00 100644 --- a/packages/router/src/navigationGuards.ts +++ b/packages/router/src/navigationGuards.ts @@ -231,7 +231,8 @@ export function extractComponentsGuards( matched: RouteRecordNormalized[], guardType: GuardType, to: RouteLocationNormalized, - from: RouteLocationNormalizedLoaded + from: RouteLocationNormalizedLoaded, + runWithContext: (fn: () => T) => T = fn => fn() ) { const guards: Array<() => Promise> = [] @@ -292,7 +293,12 @@ export function extractComponentsGuards( const options: ComponentOptions = (rawComponent as any).__vccOpts || rawComponent const guard = options[guardType] - guard && guards.push(guardToPromiseFn(guard, to, from, record, name)) + guard && + guards.push( + runWithContext(() => + guardToPromiseFn(guard, to, from, record, name) + ) + ) } else { // start requesting the chunk already let componentPromise: Promise< diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 1d6c66975..6ff152c23 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -879,7 +879,8 @@ export function createRouter(options: RouterOptions): Router { enteringRecords, 'beforeRouteEnter', to, - from + from, + runWithContext ) guards.push(canceledNavigationCheck)