原因,把接受数据的变量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