在删除项目之前,我想为我的警报提供一个提示选项。但是,它在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!");
  }
});

最佳答案

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


添加一个Promise polyfill,其中很容易获得,
不使用箭头功能,因为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将为您处理转换箭头功能。

09-25 10:24