这是我的方法:
async sendEmail() {
this.isActive = true
this.$validator.validate().then(result => {
if (result) {
await axios.post('url', data)
this.isActive = false
}
})
}
我在 isActive = true
时显示加载程序,但使用 await
里面this.$validator
向我抛出有关单词 await
的错误被预定了。为了验证我使用 VeeValidate
我如何在$validator
内使用await promise ?
最佳答案
您不能直接在非异步函数中使用 await
关键字。您直接在匿名函数中使用 await
关键字,该函数是 then
的参数;此匿名函数不是异步
,因此存在语法错误。
以下是我重写此函数的方法:
async sendEmail() {
this.isActive = true
let result = await this.$validator.validate();
if (result) await axios.post('url', data);
this.isActive = false;
}
我发现当您已经在 async
函数中时,避免使用 then
会更干净。
顺便说一句,我不确定您的 data
变量是在哪里定义的。
关于javascript - 如何在 Promise 中使用 async/await - Vue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53921045/