Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuxt : @click does not work with Nuxt-link

Tags:

vue.js

nuxt.js

I am expecting to include on my web application an effect that underlines the section where we are in the list of sections. I am working with Nuxt.

I don't know why the following code does not change the value of the boolean isActive.

<nuxt-link
  :to="`${path}/${filterItem.filter}`"
  :style='{"text-decoration": (isActive ? "underline" : "none")}'
  @click="selectSeason(filterItem.filter) toggleUnderline()" >
methods: {
  selectSeason(filter) {
    this.$router.push(`${this.path}/${filter}`)
  },
  toggleUnderline() {
   this.isActive = !this.isActive
  }
},
like image 864
thewarriorlens12 Avatar asked Sep 12 '25 07:09

thewarriorlens12


2 Answers

You could probably achieve this with something like this

<template>
  <nuxt-link to="/about" :custom="true">
    <a @click="test">go to about page</a>
  </nuxt-link>
</template>

<script>
export default {
  methods: {
    test() {
      console.log('called test method')
    },
  },
}
</script>

As shown here: https://github.com/vuejs/router/issues/846#issuecomment-808150258

Even thought, this is probably not the best approach overall and seems quite too hacky for a simple use case.
Use a button tag if you want an action, otherwise put conditional logic on your route but don't mix a client side navigation and some kind of click events.

like image 148
kissu Avatar answered Sep 16 '25 09:09

kissu


Same as router-link, you need to use v-on:click.native

<nuxt-link
  :to="`${path}/${filterItem.filter}`"
  :style='{"text-decoration": (isActive ? "underline" : "none")}'
  @click.native="selectSeason(filterItem.filter) toggleUnderline()" 
>
</nuxt-link>

How do we v-on:click nuxt-link?

like image 22
Mohammadreza Khalilikhorram Avatar answered Sep 16 '25 08:09

Mohammadreza Khalilikhorram