1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
我認(rèn)為您的部分問(wèn)題是您將編寫(xiě)代碼的異步/等待風(fēng)格與承諾鏈風(fēng)格混合在一起。當(dāng)您返回“失敗”或“成功”時(shí),您通常是從axios.get回調(diào)中返回,而不是從getSomething方法中返回。您可能想像這樣重寫(xiě)代碼:
async getSomething () {
console.log('getSomething')
if (condition) {
try {
let response = await axios.get('api_url')
console.log('getSomething success')
return 'success'
}
catch(error) {
console.log('getSomething fail 1')
return 'fail'
}
} else {
console.log('getSomething fail 2')
return 'fail'
}
}
添加回答
舉報(bào)