如何在VueJS/NuxtJs中动态调用axios方法



我正在努力优化我的代码。因此,我动态地使用axios函数,但返回的响应是一个挂起的console log。我正在使用async/await。任何人都可以帮我。

这是我的代码:

methods: {
getAgentsNames() {
const data = this.callAxios('get', `/getAllAgents`)
console.log(data) // returns pending
this.agents = data.listings
},
async callAxios(method, url, paramsBody = null) {
try {
const response = await this.$axios({
method,
url,
params: paramsBody,
headers: this.headers,
})
console.log(response) // success and have response data.
if (response.data.status == 'ok') {
return response.data
} else {
ifNotOK(response, this.$cookies)
return null
}
} catch (error) {
console.log(error)
return null
}
},
},

您的top函数还需要等待调用返回:

async getAgentsNames() {
let data = await this.callAxios(

最新更新