Closed rijenkii closed 8 months ago
It seems that because of lazy loaded routes, the actual guard is not executed with the context anymore. Once the component is lazy loaded it's no longer loaded again, that's why it only happens with beforeRouteEnter
.
I created https://github.com/vuejs/router/pull/2053 but it still needs tests. Feel free to add them. As a workaround you can force a redirect if the injectioncontext is not there:
{
template: `<div>page 1</div>`,
beforeRouteEnter: (to) => {
if (!hasInjectionContext()) {
return to.fullPath
}
console.log(hasInjectionContext())
},
}
Reproduction
https://jsfiddle.net/rv3p5ajn/13/
Code, if JSFiddle is down
```htmlSteps to reproduce the bug
Expected behavior
After all clicks value of
hasInjectionContext()
should betrue
.Actual behavior
On first and second clicks
hasInjectionContext()
isfalse
. On third and fourth, when promises are already resolved,hasInjectionContext()
istrue
.Additional information
According to the docs, all guards should have an injection context.
All other guards do have an injection context, but only before the first yield.
I think the problem lies somewhere within
runWithContext
, because it behaves weirdly with promises:Output is
true
, thenfalse
-- context is reset after first yield. Maybe the bug is invuejs/core
?EDIT: Turns out that the weird behaviour of
runWithContext
with async functions is known and kind of documented in Nuxt's docs: https://nuxt.com/docs/api/composables/use-nuxt-app#runwithcontext