1
  • nuxt.js v2.15.3.

I have used asyncData in some pages and these pages accessing by nuxt-link.
but if continuous clicking of nuxt-link (clicking interval less 1000ms), asyncData function is continuous called.
is there prevent this situation? Do i use fetch instead asyncData?

<!-- some component -->
<nuxt-link :to="`/some-page`"></nuxt-link>
// some page component 
async asyncData({ $axios ) {
  // Called as much as clicking.
  const resp = $axios.get(....)

  return { data :resp.data }
}
kissu
  • 40,416
  • 14
  • 65
  • 133
  • Perhaps disable the NuxtLink on click? Just like explained [here](https://stackoverflow.com/questions/54662327/vue-js-how-to-prevent-button-clicked-on-two-times-continuously) – Dharmaraj Jul 01 '21 at 05:44

2 Answers2

0

You can disable the link as soon as clicked:

// some component
<nuxt-link to="/some-page" @click="onLinkClick"></nuxt-link>

data() {
  return {
    linkClicked: false,
  }
}

onLinkClick(e) {
  e.preventDefault();
  
  if (linkClicked) return;

  this.linkClicked = true;
}

Of course, this solution is assuming you only want to click the link once

Bruno Francisco
  • 3,841
  • 4
  • 31
  • 61
0

fetch() may indeed help since it will move out of the page and remove the blocking effect of it.
You can also implement a debounce method or look to disable <nuxt-link> is a specific condition is met. Check my answer here for debounce: https://stackoverflow.com/a/67209352/8816585

Also, feel free to add some CSS to the button (if you want to stick with asyncData), with something like @null's solution

<nuxt-link to="/some-page" 
  @click="onLinkClick" 
  :disabled="linkClicked"
>
  My fancy link
</nuxt-link>
kissu
  • 40,416
  • 14
  • 65
  • 133