【发布时间】:2021-01-16 18:31:33
【问题描述】:
我的 Vue.js 应用程序出现以下情况:
data() {
return {
data: []
}
},
async created() {
console.log('before async call')
try {
// async call ...
console.log('after async call')
this.data = // data from api call
} catch (err) {
// handle err
}
},
mounted() {
console.log('mounted')
init(this.data)
}
当我运行代码时,我得到:
before async call
mounted
after async call
因此,作为 mount 中的类构造函数的 init 方法被调用时使用一个空数组,而不是来自 API 调用的数据。我想要的是同步执行事情并且在数据可用之前不执行挂载。我理解上面的问题是在包含异步代码的情况下Vue如何执行生命周期,但是你如何解决这样的问题呢?
【问题讨论】:
标签: vue.js