如何在不使用Promise的情况下重写swal提示

时间:2018-12-04 14:01:34

标签: javascript sweetalert2

在删除项目之前,我想为我的警报提供一个提示选项。但是,它在IE中不起作用。我如何避免使用此承诺(.then())?

swal的示例是这样:

swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});

1 个答案:

答案 0 :(得分:1)

假设其余的swal都可以在IE上运行(您检查了吗?),那么您需要:

  1. 添加一个Promise的polyfill,其中很容易实现,

  2. 不使用箭头功能,因为IE不支持箭头功能

例如,对于Promise polyfill,这是IE的有效代码:

swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then(function(willDelete) { // <=== Only change is here
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});

或者您可以使用Babel之类的工具来转换ES2015 +代码以使其与ES5兼容,并可以选择包括polyfill(用于Promise之类的东西)。 Babel将为您处理转换箭头功能。