Skip to content

Commit

Permalink
fix(guards): run beforeRouteEnter with app context
Browse files Browse the repository at this point in the history
Fix #2051
  • Loading branch information
posva committed Nov 22, 2023
1 parent 8489050 commit 34ace2c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/router/src/navigationGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ export function extractComponentsGuards(
matched: RouteRecordNormalized[],
guardType: GuardType,
to: RouteLocationNormalized,
from: RouteLocationNormalizedLoaded
from: RouteLocationNormalizedLoaded,
runWithContext: <T>(fn: () => T) => T = fn => fn()
) {
const guards: Array<() => Promise<void>> = []

Expand Down Expand Up @@ -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<
Expand Down
3 changes: 2 additions & 1 deletion packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,8 @@ export function createRouter(options: RouterOptions): Router {
enteringRecords,
'beforeRouteEnter',
to,
from
from,
runWithContext
)
guards.push(canceledNavigationCheck)

Expand Down

0 comments on commit 34ace2c

Please sign in to comment.