From 34ace2c491a65e02099de2bb15b8872071e68bce Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 22 Nov 2023 21:01:12 +0100 Subject: [PATCH] fix(guards): run beforeRouteEnter with app context Fix vuejs/router#2051 --- packages/router/src/navigationGuards.ts | 10 ++++++++-- packages/router/src/router.ts | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) 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)