我正在尝试在多个 vuejs 组件中重用一个方法。我正在使用 mixin 来执行此操作,但异步方法不起作用
我在 mixin 中有一个异步方法,它等待 axios 调用 GET-AllDrafts
完成,然后运行一个突变,将草稿状态值更新为 axios 调用返回的数据。
async orgUpdateMixin() {
await this.$store.dispatch('GET_AllDrafts')
.then(data => {
this.$store.commit('draftUpdate', data); //mutation
})
}
我在组件的 mounted()
方法中运行此方法。
mounted(){
this.orgUpdateMixin();
console.log(this.data);
}
但由于某种原因 this.data 返回 [] 空数组。有没有办法先等待 mixin 完成再继续。
最佳答案
正如评论所述,请执行以下操作:
async mounted() {
await this.orgUpdateMixin();
console.log(this.data);
}
关于javascript - Vuejs mixin 中的异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57828313/