Can't use store in router.beforeEach. State not updated #1083
-
Even if i follow the docs on using the store outside of a component and with ssr, i am not able to update store value within beforeEach navigation guard. The store seems to be available but the store value does not update. I don't understand what i am doing wrong: main.ts
localeStore
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
ok , i figured out how to "fix" this. But i don't yet understand it. When using SSR, i cannot define the navigation guard within the router index.ts nor within main.ts, i need to define it within client-entry after waiting for the route to be ready:
when i do it that way, store state is behaving as expected, but i would like to understand the "why" as I thought it should not be important where I define the navigation guard, the only requirement is, that the store usage is wrapped within a deferred call. |
Beta Was this translation helpful? Give feedback.
-
Ok, so finally i was able to fix it. For whoever might run into the same problem: Fixed version: main.ts
client-entry.ts
|
Beta Was this translation helpful? Give feedback.
Ok, so finally i was able to fix it. For whoever might run into the same problem:
The issue was that i was defining the navigation guard within createApp, but for getting the store state from SSR i waited to do that after router.isReady. That way router worked with another store state then the app. so getting the store state from SSR needs to be done before calling router.isReady(). Then everything works like a charme.
Fixed version:
main.ts