原因,把接受數據的變量this.nav寫在了調用async定義的函數外面。
錯誤代碼
initPage: async function () {
let url = 'http://examp.com/api/examp';
let params = {
code: "",
level: 0,
}
return await this.$fetch.post(url, params); // 關鍵點
},
this.nav = this.initPage();
此時,this.nav的結果就會顯示 PromiseStatus pending
正確代碼
initPage: async function () {
let url = 'http://examp.com/api/examp';
let params = {
code: "",
level: 0,
}
this.nav = await this.$fetch.post(url, params); // 關鍵點
},
this.initPage();
除非使用.then()來接收結果
this.initPage().then(res => this.nav = res);
https://www.jianshu.com/p/aaef3ab70b8f
https://stackoverflow.com/questions/47259953/js-async-await-return-promise?r=SearchResults