使用async await,await的函数log出了数据,但是async函数获取不到值,并且先打印出undefined,在打印出数据 getRechargeOrder () {
let apiUrl = this.$api.URL.rechargeOrder this.$api.axiosGet(apiUrl, para).then(res => { if (res.data.code === 0) {
let data = res.data.data
if (data.payNum) {
console.log(data)//{payTotal: 3344, payNum: 86, payPeopleNum: 65, list: Array(86)}
return data
}
}
}).catch(error => alert('error'))
},
async getDau () {
let res = await this.getRechargeOrder()
console.log(res)//undefined
}
1 回答
互换的青春
TA贡献1797条经验 获得超6个赞
你的getRechargeOrder ()
里面又没有返回值 当然是undefined
啊,正确的做法是getRechargeOrder ()
返回一个Promise
,得到数据后触发resolve
getRechargeOrder() { return new Promise((resolve, reject) => { let apiUrl = this.$api.URL.rechargeOrder this.$api.axiosGet(apiUrl, para).then(res => { if (res.data.code === 0) { let data = res.data.data if (data.payNum) { console.log(data)//{payTotal: 3344, payNum: 86, payPeopleNum: 65, list: Array(86)} resolve(data); } } }).catch(error => { reject(error) }) }) }
添加回答
举报
0/150
提交
取消