typescript 无需交互即可使用 SweetAlert2 显示加载警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43957637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:32:56  来源:igfitidea点击:

Show loading alert with SweetAlert2 without having to interact

typescriptpromisesweetalertsweetalert2

提问by Vic

Right now I have this code:

现在我有这个代码:

swal({
     title: 'Loading cars from data base',
     showLoaderOnConfirm: true,
     preConfirm: () => {
       return this.carsSvc.getCars().then((cars: ICar[]) => {
               this.setData(cars);
               resolve();
            });
    }
});

This code shows an alert, I have to press 'confirm' and then it shows a loading icon and closes when the loading is finished. I want only the last part of this action, so I don't want to have to confirm the loading before it begins to load. I want the loading to start when the alert is opened and to close automatically when the loading is finished. Is this possible with SweetAlert2?

此代码显示一个警报,我必须按“确认”,然后它会显示一个加载图标并在加载完成后关闭。我只想要这个动作的最后一部分,所以我不想在开始加载之前确认加载。我希望在打开警报时开始加载,并在加载完成时自动关闭。SweetAlert2可以实现吗?

回答by Vic

A little late probably but I figured this out. Try this:

可能有点晚了,但我想通了。试试这个:

swal({
     title: 'Loading cars from data base'
});
swal.showLoading();

this.carsSvc.getCars().then((cars: ICar[]) => {
           swal.close();
           this.setData(cars);
           // show a new success swal here?
        });

You want to call the swal.showLoading()method to disable buttons. You can then call swal.close()to finish.

您想调用该swal.showLoading()方法来禁用按钮。然后您可以调用swal.close()完成。

回答by Hassan Fayyaz

you can also use this

你也可以用这个

            Swal.fire({
                title: 'Please Wait !',
                html: 'data uploading',// add html attribute if you want or remove
                allowOutsideClick: false,
                onBeforeOpen: () => {
                    Swal.showLoading()
                },
            });

and for close It

和关闭它

swal.close();