请教关于 JS 的 await 的问题,应该没那么幼稚
公司就我一个开发,后端被逼成了全干,现在前端越来越复杂,头大。
找了个 VUE 的模板。echarts 想用 ydata 接收后端传来的值,定义了取值的方法 fetchData,因为是 axios 异步的,在 drawLine 调用 await 修饰的 fetchData,然而 console.log ydata 是空的,感觉是一个 await 不管用,还有 then 的影响没消除。 但是我不调用 fetchData(),直接在 drawLine()里面使用 await this.$axiso.post(…),却成功给 ydata 赋值了。看起来跟直接调用 fetchData 没什么区别,理解不了。
methods: { fetchData() { this.$axios .post(“/controllermsgdatelist”, this.listQuery) .then(response => { this.ydata = response.data.result; }); },
async drawLine() { await this.$axios .post("/controllermsgdatelist", this.listQuery) .then(response => { this.ydata = response.data.result; }); // await this.fetchData(); console.log(this.ydata);
}