使用 Nuxt 点击时调用 api
call api on click with Nuxt
如何使用事件处理程序调用 api,例如使用 Nuxt 单击按钮时?我了解 fetch()
和 asyncData()
挂钩,但它们是在服务器端呈现时调用的。但是,如果我想在单击以下按钮时调用 API 怎么办:
//template
<button @click="myMethod"> click me </button>
// script
methods:{
myMethod(){
// call my api here
}
}
例如使用 nuxt axios module:
<button @click="myMethod"> click me </button>
methods:{
myMethod() {
this.$axios.get('some-api-url').then(response => {
console.log(response)
})
}
}
如何使用事件处理程序调用 api,例如使用 Nuxt 单击按钮时?我了解 fetch()
和 asyncData()
挂钩,但它们是在服务器端呈现时调用的。但是,如果我想在单击以下按钮时调用 API 怎么办:
//template
<button @click="myMethod"> click me </button>
// script
methods:{
myMethod(){
// call my api here
}
}
例如使用 nuxt axios module:
<button @click="myMethod"> click me </button>
methods:{
myMethod() {
this.$axios.get('some-api-url').then(response => {
console.log(response)
})
}
}