-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Is there any way to force navigation when clicking on a router-link even if the route doesn't change? #2430
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
You can reload the page, but you should handle the refresh in a click handler if you want to have that custom behaviour Duplicate of #974 |
@posva How do you deal with that? |
yeah, they are not supposed to be triggered as you are in the same route with no param change or whatsoever... You need to refactor the code in methods and call them in both places. Cheers |
[Solved] I create a base component, and use it instead of You can change the query name to whatever you want, and use the timestamp or some unique string to make the <template lang="html">
<router-link v-bind="$attrs" :to="to" @click.native="clickHandler">
<slot></slot>
</router-link>
</template>
<script>
export default {
name: 'BaseLink',
props: {
to: {
type: [Object, String],
default: '/',
},
},
data() {
return {
currentRoute: null,
};
},
created() {
this.currentRoute = this.$route.name;
},
methods: {
clickHandler($event) {
if (!($event.ctrlKey || $event.metaKey) && this.currentRoute === this.to.name) {
const query = { ...this.$route.query, _: new Date().getTime() };
const newRoute = { ...this.$route, query };
this.$router.replace(newRoute);
}
},
},
};
</script> |
I doubt this method, it looks dirty and contaminate the history (and generate no real-meaning query in the url). To be clear, you don't want a "real, F5 like" refresh since your script have to be reload and the browser have to reparse it, right? (The OP's solution is in that direction.) More simple way is to assign an unique |
Yeah, I've tried to bind a key to <router-view :key="$route.fullPath"/> Since the Can you give me an example, plz. Talk is cheap, you know. |
You can have something like <router-view :key="$route.params.theNameYouGive"/> And you manipulate like: <router-link :to="{ params: { theNameYouGive: Data.now } }" replace> ... </router-link> Remember |
@leoyli |
@trandaison That is to say in any cases URL have to be change unless you assign key to an internal state ( If you still want to use the approach by changing URL, I suggest you manipulate |
Try:
|
I've just ran into this problem also. Use case:
|
Same problem.
|
@posva is there a technical limit? I do not see how this would not be the desired default effect. |
@cafe4it but doing so your app is not SPA anymore, the whole app will be reload again. |
This is my trick to get through this situation:
Everything works just fine so far. |
I have a nice trick. It works with First, you register a click event on your link component After that, you check in the click handler if you are on the same route if(this.$route.path === '/home') {
// do some staff here
// example
window.scrollTo({ top: 0, behavior: 'smooth' })
} The condition will be true only when you click the navigation link for the same route |
I'm interested in having some sort of functionality here that does this natively. |
@posva Can this be re-opened? There seems to be a lot of community support for a option to force a re-render. Please see the use case I shared above, I would love the option to force without the additional complexity of adding a watcher to accomplish something so simple. Thanks in advance. |
I added a query param that includes current time as milliseconds and it's working for me. this.$router.push({ name: 'post', params: { type: 'detail' }, query: { id: 123, time: Date.now() } }); |
What problem does this feature solve?
Reload current page's state or refresh the page.
For example, I have a
<HeaderBar>
which contains aHome
button, just like the Facebook one.If I'm in another page (rather than Home), when I hit this button, it will navigate to Home. But when I'm in Home page, hit this button it does nothing.
It's expected to reload the page, and scroll to the top, right?
Please don't tell me to write out an action to refresh the state and scroll to top. This header bar is a shared component, and the Home button can be hit at any where.
What does the proposed API look like?
Some thing like
or
The text was updated successfully, but these errors were encountered: