简体   繁体   English

如何让sweetalert返回true / false以确认没有承诺?

[英]How to have sweetalert return true/false for confirm without promise?

I like the simplicity of javascript confirm so that I can do this: 我喜欢javascript确认的简单性,所以我可以这样做:

if(!confirm("are you sure?"))
    return;

in sweetalert you have to nest your true code within a then promise function. sweetalert您必须将您的true代码嵌套在then promise函数中。

Is there a way to have sweetalert return true/false the same way it happens in JS confirm? 有没有办法让sweetalert返回true/false ,就像JS确认的那样?

By design, SweetAlert uses Promises to keep track of how the user interacts with the HTML-based alert and it's non-blocking (you can still interact with the webpage/UI) as opposed to the browser's built-in confirm() method, which, when displaying the modal window, it prevents the user from accessing the rest of the program's interface until the dialog box is closed . 根据设计, SweetAlert使用Promises来跟踪用户如何与基于HTML的警报进行交互,并且它是非阻塞的 (您仍然可以与网页/ UI进行交互) ,而不是浏览器的内置confirm()方法, ,当显示模态窗口时, 它会阻止用户访问程序界面的其余部分,直到关闭对话框


You cannot call swal() with the same interface-blocking behavior, as if it were another type of confirm() . 不能使用相同的接口阻塞行为调用swal() ,就好像它是另一种类型的confirm() However , by using the ES2017 async / await features, you can write your code in a similar fashion and practically achieve the same goal without blocking the interface. 但是 ,通过使用ES2017 async / await功能, 您可以以类似的方式编写代码,实际上可以实现相同的目标而不会阻止接口。


In order to be able to use async / await across browsers, use a transpiler (eg Babel ) to transpile/convert your source code with ES2015+ features to ES5 , which is widely supported : 为了能够跨浏览器使用async / await ,使用转换器(例如Babel将带有ES2015 +功能的源代码转换/转换为ES5 ,这得到了广泛的支持

- Using swal() in an if statement without wrapping: - 在if语句中使用swal()而不包装:

You can just simply call swal(...) with await : 你可以简单地用await来调用swal(...)

if (!await swal({text: 'Are you sure?', buttons: true})) {
    return;
}

And the Promise will resolve, when using SweetAlert as truthy ( true , when the user confirms the alert) or falsy ( null otherwise) as the condition of the if statement as described in the SweetAlert guides . 当使用SweetAlert作为真实 true ,当用户确认警报时)伪造 (否则null作为SweetAlert指南中描述的if语句的条件时, Promise将解决。


- Using swal() in an if statement with a wrapper to resemble to confirm() : - 在if语句中使用swal() ,使用包装器类似于confirm()

In order to provide the familiarity of confirm() , separate swal(...) with the desired options into an async function : 为了熟悉confirm() ,将带有所需选项的swal(...)分离到async function

async function confirm(message) {
    return swal({
        text: message,
        buttons: true
    });
}

Then use it in the if statement prefixed with await as if it were just like a form of confirm() and as such, it would also work as expected: 然后在带有await前缀的if语句中使用它,好像它就像一个confirm()形式,因此,它也可以按预期工作:

if (!await confirm('Are you sure?')) {
    return;
}

Things to consider: 需要考虑的事项:

  • Using await outside of an async function is currently not supported . 目前不支持async function之外使用await To get around this issue, either place your code in an event handler: 要解决此问题,请将代码放在事件处理程序中:

     document.addEventListener('DOMContentLoaded', async () => { // ... await and other async code here }); 

    or use an async IFEE or IIAFE : 或使用async IFEEIIAFE

     (async () => { // ... await and other async code here })(); 
  • In order to easily transpile your source code containing ES2015+ features, consider using a tool or library to bundle your code (eg Webpack , Browserify , Gulp , Grunt , etc.) without much effort in the long run. 为了便于转换包含ES2015 +功能的源代码,请考虑使用工具或库来捆绑您的代码 (例如WebpackBrowserifyGulpGrunt等) ,从长远来看不需要太多努力。


Working example with quick setup: 快速设置的工作示例:

You can check out the sources of the working example in this repository based on Webpack . 您可以根据Webpack查看 此存储库中工作示例的来源。

Full disclosure: I made the repository in order to provide an easily usable example, which can be cloned, install dependencies via NPM and start using right away. 完全披露:我创建了存储库,以便提供一个易于使用的示例,可以克隆,通过NPM安装依赖项并立即开始使用。


Additional resources: 其他资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM