-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Force reload when clicking on a router-link even if the route doesn't change #1257
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
Comments
Duplicate of vuejs/vue-router#974 |
Note you can pass a random query parameter to force a navigation: router.push({ query: { _r: Date.now() }}) |
Already tried that, but it's a hack and it will just pollute the url, I am looking for a cleaner solution.
I am not sure that's a duplication since it's on a different repo and it's not the same version, more than that it's opened since 2016, i think it's completely ignored by Vue core team. |
An undocumented argument called Lines 78 to 81 in 87d09f8
@posva Perhaps it's worth adding to the documentation? |
It's not documented for a reason 😉 . Don't rely on it as it could be removed in the future. |
Let's hope not 😄 |
What do you think about doing this?
|
Was annoyed by a slightly different use case: fetching data in Official documentation suggests a pattern of having to If you don't rely on params and different URLs, you can create a global variable, assign a random value in one of the navigation guards, and use that as a key for router view. This way URL doesn't get polluted, and the components are correctly mounted/unmounted on every navigation. You can also customize this behaviour by placing the |
many thanks @inca 🙏 |
BTW, you don’t need the repetition: using one watch with immediate true is enough (no key on router view) |
Interesting. So, what I'm about to say is probably very controversial in Vue community (and kind of off topic), so sorry in advance. IMO/YMMV implied everywhere. But here goes:
I guess the justification for my case is that I only have 3 level nesting, and re-mounting matters only on the last level. In my head even though it's a hack it's still applied only on a couple of layouts (as opposed to every route component) and therefore is much better localized than |
How to do this and keep query parameters? |
The simplest solution is to add a :key attribute to : // Vuejs 2 // Vuejs 3
This is because Vue Router does not notice any change if the same component is being addressed. With the key, any change to the path will trigger a reload of the component with the new data. |
The solutions from @edmidev seems to work fine. I was trying to use the "force" parameter from the docs like this:
But is not working (at least for me or my setup). Also, I tested the watcher way and seems to work too. Just for a bit of context. In my case, I have different routes for pages that build up dynamically and share the same DefaultView (kinda similar to a page builder). So, when the page loads, I read my page builder config three and load all the content block components. I'm using v4.3.3 of the router. It kinda feel that the right way is to force the reload like when reloading a page but I will stick to this workaround for now. |
@edmidev awesome, this seems to work for me as well. Does anyone know if there's any performance impact to it? |
Hi @kevinkosterr, well using the version with :key is the most effective and straightforward solution to reload a route, but you should keep in mind that this reloads the component as if it were mounted for the first time. It only has an impact if the component has a heavy onCreated or onMounted event that makes API calls or performs complex rendering. |
@edmidev Ofcourse, thanks. I just changed mine to |
Version
4.0.12
Reproduction link
codesandbox.io
Steps to reproduce
Simply add a router-link to a view that points to the current page. Then click on that link multiple times, the page or the router-view does not reload or refresh
What is expected?
Refresh or reload the router-view ( same as any HTML a tag )
What is actually happening?
Nothing
The text was updated successfully, but these errors were encountered: