【发布时间】:2021-03-11 23:36:27
【问题描述】:
我有一个对话框组件,它在提交时执行两个异步函数。我的目标是保持对话框打开并显示加载状态,直到两个功能都完成。之后,我想关闭对话框。
我在父组件中定义的提交函数如下所示:
async submit() {
await this.foo1();
await this.foo2();
}
这个函数作为一个 prop 传递给对话框组件:
<app-dialog @submit="submit" />
在我的对话框组件中,单击按钮时,我尝试这样做:
async onClick() {
await this.$emit('submit');
this.closeDialog();
},
但是,对话框会立即关闭,而不是等待执行提交。完成此任务的最佳方法是什么?
【问题讨论】:
-
this.$emit是否返回一个可以等待的 promise/thenable?如果不是,我会假设 await 什么都不做 -
@evolutionxbox 很遗憾没有,我在尝试时得到了
"TypeError: _this.$emit(...).then is not a function" -
_this.$emit(...).then这看起来您的转译器正在用then替换 await 语法? -
Nono,当我尝试做
this.$emit('submit').then(() => // code)时我明白了
标签: javascript vue.js async-await