封装 axios 请求 并 return 结果

axios请求是异步请求,我遇到的情况是多个请求用同一个地址,通过传参不同控制想要的返回结果,因此,这时想将axios请求封装为一个函数,通过return将返回结果传出来。

但是如何实现呢,我才用的方法如下:

queryData(参数..){
  var datas={
    定义请求参数..
  }
  return axios.post(this.url,datas).then(response=>response.data.result).catch(error => error)
},

  这里的返回值为promise形式,因此采用.then()获取值。

this.queryData(参数..).then(response=>{
        console.log(response)          
 })    

  就可以拿到想要的值了。

猜你喜欢

转载自www.cnblogs.com/baoziqiaodaima/p/12079656.html