Skip to content

A route component should unmount before navigation happens #3538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sto3psl opened this issue Apr 28, 2021 · 2 comments
Closed

A route component should unmount before navigation happens #3538

sto3psl opened this issue Apr 28, 2021 · 2 comments

Comments

@sto3psl
Copy link

sto3psl commented Apr 28, 2021

Version

4.0.6

Reproduction link

https://codesandbox.io/s/awesome-albattani-b1uvt

Steps to reproduce

In the linked CodeSandbox open the console and follow these steps:

  1. Click on Route 2 link
  2. Click on Home link
  3. Check URL bar and console output

What is expected?

After navigation from /route to / I should see the correct URL in the URL bar -> / and the console output should be:

Route.vue unmount
Navigated to: /
HelloWorld.vue mounted

What is actually happening?

After navigation from /route to /, the URL bar shows /?test=hello and the console output is:

Navigated to: /
Route.vue watchEffect: /
Route.vue unmount
HelloWorld.vue mounted


By having a watchEffect that depends on the route value this bug causes code to run after a route change happened and can trigger unintended side effects.

@sto3psl
Copy link
Author

sto3psl commented Apr 28, 2021

I think this is related to #3393

@posva
Copy link
Member

posva commented Apr 28, 2021

Component mounting is not directly connected to navigation and will never be because there are too many scenarios where it's not always possible. In most scenarios, you can just wait for nextTick():

router.afterEach((to) => {
  console.log("Navigated to: ", to.path);
  nextTick().then(() => {
    console.log('actual navigated to')
  })
});

Remember to use the forum or the Discord chat to ask questions!

@posva posva closed this as completed Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants