1 回答

TA贡献1818条经验 获得超11个赞
所以我看到在显示我需要的数据之前,我需要等待 Axios 调用完成。现在的问题是,我如何在 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)
}
}
添加回答
举报