Reputation: 1150
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
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