vuejs2 - 如何解决:“不能在异步函数之外使用关键字‘await’”
问题描述
在我的 Vue 组件中。我有一个显示 sweetalert2 输入消息的方法(显示)。但是使用 laravel mix (npm run watch) 后返回错误
show(){
const { value: email } = await Swal.fire({
title: 'Input email address',
input: 'email',
inputPlaceholder: 'Enter your email address'
})
if (email) {
Swal.fire('Entered email: ' + email)
}
}
错误是:
不能在异步函数之外使用关键字“等待”
解决方案
我不确定你对 show 方法的调用语法,但你需要声明该函数async
才能使用await
.
例如,在模块方法中:
const show = async () => {
const { value: email } = await Swal.fire({
title: 'Input email address',
input: 'email',
inputPlaceholder: 'Enter your email address'
})
if (email) {
Swal.fire('Entered email: ' + email)
}
}
作为类中的实例方法:
class MyComponent {
async show() {
const { value: email } = await Swal.fire({
title: 'Input email address',
input: 'email',
inputPlaceholder: 'Enter your email address'
})
if (email) {
Swal.fire('Entered email: ' + email)
}
}
}
推荐阅读
- python - 如何根据两个不同的列表计算值计数?
- python - 使用 Python 使用 BeautifulSoup 进行网页抓取
- amazon-web-services - aws sam local 如何修复错误 ResourceNotFoundException
- python - 我需要一个主要计划文件,并且在其中我还需要运行一个子计划文件,我该怎么做?我尝试使用 crontab 来安排主要计划
- javascript - 从对象打印一定数量的属性
- javascript - 如何使用正则表达式进行数组拆分?
- django - 如何在 django 中上传之前预览图像
- python-3.x - 如何使用 aiter() 和 anext() 内置函数使用异步迭代器
- javascript - 有没有办法在使用 reactJs 加载屏幕时播放声音?
- java - 如何在 Java 中检索特定的对象字段?