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
@click listener on <nuxt-link> or <router-link> no longer works inside page components on Vue 2.7 #10593
Comments
Same issue here on Node 16.16.0; Updating from Vue 2.6.14 to 2.7.8 breaks the native click handlers. The handlers also seem to work fine when there is no navigation, e.g. when a NuxtLink leads to the current route. Seemingly navigation cancels handler execution. |
having the same problem as i wondered why all the sudden my app was behaving strange. ended up wrapping the slot in a span and adding the click-handler to it instead of binding it to nuxt-link. edit: Also my wokaround doesn't really always work that great |
I also could narrow down that this problem only pops up if the click on nuxt-link is referring to a page with a different layout than the current page. |
Please refer to https://stackoverflow.com/a/74561723/19901666 for Nuxt3. |
Hello, Before:
After:
into the handler function:
Now you can do what you want and then navigate, Hope this quick fix helps "^^ |
I ran in to a similar issue on our I put together a Nuxt 2 example, and added a solve that should work for most people. It's a little more work up front, but should make the whole process relatively invisible once you implement it. Instead of doing programmatic navigation, you can create a wrapper component and just proxy the click events to an inner element (like a Something like this: <template>
<NuxtLink v-bind="$attrs"> <!-- bind attrs like class, etc to the root -->
<span v-on="$listeners"> <!-- bind listeners to this inner element -->
<slot />
</span>
</NuxtLink>
</template>
<script>
export default {
name: 'BaseLink',
inheritAttrs: false, // prevent events from binding to the root element
};
</script> Reproduction on StackBlitz, https://stackblitz.com/edit/nuxt-starter-aaaqgu?file=components/Navigation.vue |
Any work-around while this gets fixed? 👀 |
#10593 (comment) does not seem to correspond to key operation.
Maybe we can do it this way. <template>
<a
href="/path-to-another-layout-page"
@click="
(e) => {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
e.preventDefault();
$router.push('/path-to-another-layout-page');
}
"
>
Another layout page
</a>
</template> |
As pointed out above - thanks for the very helpful investigation! - this is not a Nuxt issue but a change in Vue 2.7 or Vue Router. Would you raise an issue upstream in one of those repos? Possibly linked: vuejs/vue#12781. |
Versions
Reproduction
https://codesandbox.io/s/happy-mendeleev-2klf2i?file=/pages/index.vue
Additional Details
Works in Vue 2.6, given the following page component
Steps to reproduce
Upgrade an existing Nuxt 2 app to Vue 2.7 or create a new one.
@click.native
handlers on nuxt or router links no longer function as they did in 2.6, but apparently only broke in page components or children thereof. They work fine when put directly in layout. It seems like the click handler is no longer executed before the page component gets destroyed, or something else might have changed. I could not reproduce the same issue using Vue-Router 3.5.4 on a non-Nuxt Vue 2.7 app. The interesting thing is thatclick.prevent
orclick.native.prevent
also do not prevent a route change, but I did not test if this ever even worked in 2.6.What is Expected?
The event handler is executed
What is actually happening?
The event handler is only executed some times (in layout, see repro)
The text was updated successfully, but these errors were encountered: