Internet Explorer 11 上的 SweetAlert2 语法错误

SweetAlert2 syntax error on Internet Explorer 11

我正在使用 SweetAlert2 examples page:

的确切代码
swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then((result) => {
  if (result.value) {
    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    )
  }
})

在 Firefox 和 Chrome 上工作正常,但 Internet Explorer 显示 SCRIPT1002: Syntax Error 而不是 运行 脚本...IE 将此部分标记为语法错误:

}).then((result) => {

感谢您的帮助

(result) => {} 是箭头函数,IE 完全不支持。要解决此问题,您必须使用传统的匿名函数:

swal({
  // options...
}).then(function(result) {
  if (result.value) {
    swal('Deleted!', 'Your file has been deleted.', 'success');
  }
});

IE11 不支持某些现代 ES6 功能,例如 arrow functions and promises

要修复它,您应该使用 Babel 编译代码,或者使用具有传统 function 语法的 Promise-polyfill

swal(...)
  .then(function(result) {
    console.log(result.value)
  })

阅读更多关于 SweetAlert2 用法的信息:https://github.com/sweetalert2/sweetalert2#usage

除了匿名函数之外,为了让 swal 在 IE 中完全发挥作用,它必须添加和脚本标签

<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/core.js"></script>

https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2#1-ie-support

要使 showLoading() 在 IE11 中工作,您需要使用 promise shim 和一个匿名函数...

Swal.fire({
    title: "Saving",
    text: "Please wait...",
    onBeforeOpen: function() {
        Swal.showLoading();
    }
});