Promise function in async setup still execute when switch router
minhtien020900 opened this issue · comments
minhtien020900 commented
Reproduction
https://stackblitz.com/edit/vitejs-vite-zjpnie?file=src%2Fcomponents%2FHome.vue
Steps to reproduce the bug
- Step1: From page home, switch to /blog
- Step2: When page blog show loading, back to home
- Step3: Wait a few seconds, router will push '/about'
Expected behavior
- delay() function will be stopped as soon as when switch router
- Router not push to /about
Actual behavior
- delay() still executes even though the router has changed.
- Router is redirected to /about when current router is /home.
Eduardo San Martin Morote commented
Thanks for the great reproduction! The behavior you are seeing isn't related to the router, it's just how things work with Suspense (and in JS in general). The watcher being immediate, executes even though the component never manages to mount (it doesn't even unmount). The solution is to move the watcher within an onMounted()
to ensure this only runs if the component ever mounts