使用Promise或async/await异步弹窗问题ElMessageBox
时间: 2023-09-20 22:09:29 浏览: 332
你可以使用以下代码来使用Promise异步弹窗问题:
```
const messageBox = (title, message, type) => {
return new Promise((resolve, reject) => {
ElMessageBox.confirm(message, title, {
type: type,
showCancelButton: true,
center: true
}).then(() => {
resolve(true); // user clicked "OK"
}).catch(() => {
resolve(false); // user clicked "Cancel"
});
});
};
// Example usage:
async function example() {
const result = await messageBox("Confirm", "Are you sure you want to delete this?", "warning");
if (result) {
// user clicked "OK", perform deletion
} else {
// user clicked "Cancel", do nothing
}
}
```
弹窗将返回一个Promise,您可以使用async / await语法对其进行操作,并根据返回值确定用户是否单击了确定或取消按钮。
希望这能帮助您!
阅读全文