seyet
seyet

Reputation: 1150

call api on click with Nuxt

How do I call an api with event handlers like when clicking on a button with Nuxt? I understand the fetch() and asyncData() hooks but they are called on server side rendering. But what if I want to call an API when I click on a button like:

//template
<button @click="myMethod"> click me </button>

// script
methods:{
  myMethod(){ 
    // call my api here
  }
}

Upvotes: 0

Views: 2699

Answers (1)

S&#248;lve
S&#248;lve

Reputation: 4406

For example using the nuxt axios module:

<button @click="myMethod"> click me </button>
methods:{
  myMethod() { 
    this.$axios.get('some-api-url').then(response => {
      console.log(response)
    })
  }
}

Upvotes: 3

Related Questions