-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Detect when users click an exact active router-link
(current page === router-link's to)
#1155
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
Hey, you can actually add an event listener like |
@posva hmm seems he's asking how to only trigger a function when you click on the same route, in your example when I click |
|
@egoist Yes, you're right. In that case the user can check that the route is the same in the click event. Maybe we can fire a new event when the user clicks on the router-link while it's the current route. But currently, you can achieve that on userland already |
@posva I've tried to check if the route is the same in the click event, but it seems that the event is fired after navigation, because when I tried to compare current route with the route from link they were same all the time. P.S: For now, I've solved my issue from the first post with a wrapper component for |
seems no, as per doc: |
Oh, nevermind ^^ |
So maybe reopen this? |
Why does it have to be the second time? Why not simply scroll to the top on every click? |
Yes, the fact that the click triggers after the navigation mean that you have to manually handle everything or use a query like |
router-link
many times.router-link
(current page === router-link's to)
@fnlctrl Imaging you're reading a news feed, and click on one of the news to get more details, then you want to go back, but instead of doing back, you click on the news button because it's there, we may want to scroll to where the user was last time he was on the news feeds. I'll personally hit Back, but some people forget about this really often. Also, in a PWA on an iPhone, you may not have a back button |
I think we're complicating the problem here... <router-link to='/foo' @click.native="toTop"/> methods: {
toTop: {window.scrollTo(0,0)}
} @posva That's not what OP originally asked for. From my perspective, rendering a dedicated a back button would be most appropriate for this use case. |
I didn't express myself correctly. I also think that way 😆 |
@fnlctrl Yes, you're right, and your example shows that I was trying to acheive, but, for now, you will have to repeat that As I wrote in another comment, I've solved this with a wrapper component for P.S: Also, the sensation of 'dead' links is not user firendly, because when user clicks a link (even the second time), he/she expects at least something to happen (scroll, in our case). |
Well, I think duplicating
<div class="nav-bar" @click="toTop">
<router-link ....../>
<router-link ....../>
<router-link ....../>
...
</div>
Vue.prototype.$scrollToTop = () => window.scrollTo(0,0) And it can be used everywhere including templates. <router-link @click.native="$scrollToTop"> |
What if I don't really want to |
...I guess you can just don't bind a click event? <div class="nav-bar" @click="toTop">
<router-link @click.native.stop ....../> // now clicking this won't scroll to the top
<router-link ....../>
<router-link ....../>
...
</div>
Then it's not suitable to be handled by |
@fnlctrl In that case, to my mind, a wrapper component seems to look better, and we can use it like <template lang="pug">
router-link(
:to="to",
:tag="tag",
:exact="exact",
:append="append",
:replace="replace",
:activeClass="activeClass",
:event="event",
@click.native="scroll"
): slot
</template>
<script>
export default {
props: [ /* .. */ ],
methods: {
scroll() {
window.scrollTo(0,0);
}
}
}
</script> |
Yeah, it looks nice indeed. Closing as the issue is now resolved in userland ;) |
I am having the same problem while trying to implement a menu which comes back to home when clicking on the active link. And like it was mentioned I cannot compare the link's When searching the documentation, I was expecting a callback like |
See #974 |
It would be great if we will be able to at least detect when our users click the same
router-link
again.Let me explain:
Let's say I have an app with fixed navbar on top and a user have navigated to a long page (let it be
/news
) and at one moment after long scrolling (let the page have infinite scroll) the user decides to click the same link/news
again (from the navbar), obviously to go up (or to refresh the page, who knows), but the problem is that nothing will happen (it feels like the link is dead), and as far as I know there is no way to capture multiple clicks on the sameroute-link
without using some hacky workarounds.The problem can be solved for example by adding a timestamp to query string so every time user clicks the same link the navigation will happen again, but it feels so wrong, because the only thing basically what should happen is the scroll to an anchor or scroll to the top.
So, it would be great to have an option to detect when users click the same
router-link
multiple times without hacky workarounds, or at least an option to scroll to the top on secondary click.If there is an elegant option to do it now, please tell me, because I didn't find such feature in the documentation.
The text was updated successfully, but these errors were encountered: