question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vue-router current path invalid for a short time when loading async component

See original GitHub issue

Version

3.0.3

Reproduction link

https://codesandbox.io/s/y32jkmjr7v

Steps to reproduce

Using vue and vue-router, create a route that requires a component (componentAsync) that is async loaded from the main component (componentApp).

Do something that uses router.push or router.go inside of mounted() of the componentApp without a path or name. For example, router.push({query: { test: 'foo' }})

The redirected route will just use path: ‘/’ instead of the actual path you used to initially hit the page.

For example, if you hit domain.com/about, you will wind up at domain.com/?test=foo, instead of domain.com/about?test=foo

What is expected?

vue-router would use the current window location

What is actually happening?

vue-router is using /


This is because the async component makes the confirmTransaction callback take longer than the mounted() call to router.push, so the route inside router is still the one with the wrong path.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
719mediacommented, Apr 12, 2019

Thanks for the review. I disagree with the reasoning, as a developer, I would not want to have to think about additional criteria. I want to just be able to say $router.push(), not “Can I say $router.push yet, or do I need to wait for onReady”? This means potentially I need to wrap any $router.push inside of a onReady() if I want to be 100% safe, which just seems… lame. But, I understand that there are reasons beyond my understanding for your decision.

1reaction
Tofandelcommented, Apr 10, 2021

Well apparently you can delay mounting the app without any issue

Vue.use(router);

const v = new Vue({
  router,
  render: //...
});

router.onReady(() => {
  v.$mount('#app');
});

Just don’t wrap the new vue or the router will never be ready and none of the code will run

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Using async components with loading and error ...
I came across the answer to this question by digging through github issues on vue-router. It turns out you can't just use async...
Read more >
Waiting for the result of a Navigation - Vue Router
A new navigation guard takes place while the previous one not finished. ... are asynchronous, we need to await the promise returned by...
Read more >
A Beginner's Guide to Vue Router - SitePoint
Learn the basics of using Vue Router, such as router setup, passing custom parameters, page navigation, and implementing a 404 page.
Read more >
Using Vue 3's Router in Practice - Daily.dev
Now you have a Vue 3 project configured with Vue Router, and let's see the features available and how to implement them. Nested...
Read more >
Beginner Vue.js Tutorial with User Login - Auth0
It will also install the dependencies, so give it a little time (~30 seconds). ... Vue itself; The App component (which we'll go...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found