首页 > 编程知识 正文

axios的返回值,vue中使用axios

时间:2023-05-05 05:02:50 阅读:272292 作者:2243

原因:

因axios返回值是异步操作,获取返回值时,请求操作还未完成,就已经执行了赋值,导致结果位undefined

解决方法:

async….await,async声明发放为异步方法,await等待异步操作执行完毕

举例: async getCountry(city){ let arr=[] await this.$axios.post(baseUrl+'/county2.do',qs.stringify({ cityCode:city })).then(res=>{ arr=res }) return arr }

异步方法返回值是promise,所以接收方法如下:

this.getCity(code).then(res=>{ console.log(res);})

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。