因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);})