1 回答

TA貢獻1818條經驗 獲得超11個贊
所以我看到在顯示我需要的數據之前,我需要等待 Axios 調用完成?,F在的問題是,我如何在 Vue 中做到這一點?
我假設您需要在設置之前等待axios.get()
調用getApiData()
完成this.activeDisplay
。為此,首先返回axios.get()
(ie, a Promise
) 的結果,以便調用者可以await
得到它。
getAPIData(id,type) {
// BEFORE:
//axios.get(...)
// AFTER:
return axios.get(...)
}
然后制作setActiveDisplay()
一個async
函數,允許編輯的getApiData()
結果await
:
// BEFORE:
//setActiveDisplay: function (id, ad) {
// AFTER:
async setActiveDisplay(id, ad) {
if (...) {
await this.getApiData(...)
}
this.activeDisplay = ad
}
或者,您可以不使用async
/await
并使用Promise.prototype.then()
:
setActiveDisplay: function (id, ad) {
if (...) {
this.getApiData(...).then(() => this.activeDisplay = ad)
}
}
添加回答
舉報