繁体   English   中英

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

[英]How to dynamic call the axios method in VueJS/NuxtJs

我正在尝试优化我的代码。 所以,我动态使用 axios function,但返回的响应是一个挂起的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
    }
  },
},

您的顶级 function 也需要等待调用返回:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM