Uncaught TypeError: Cannot read property 'then' of undefined using Sweet Alert

Uncaught TypeError: Cannot read property 'then' of undefined using Sweet Alert

我正在使用 SweetAlert2,但出现以下错误

Uncaught TypeError: Cannot read property 'then' of undefined

当我使用与 SweetAlert 页面中建议的完全相同的代码时。

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!',
  cancelButtonText: 'No, cancel!',
  confirmButtonClass: 'btn btn-success',
  cancelButtonClass: 'btn btn-danger',
  buttonsStyling: false,
  closeOnConfirm: false,
  closeOnCancel: false
}).then(function(isConfirm) {
  if (isConfirm === true) {
    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    );
  } else if (isConfirm === false) {
    swal(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    );
  } else {
    // Esc, close button or outside click
    // isConfirm is undefined
  }
})

我在页面末尾有代码。我在 Rails 应用程序的 Ruby 中使用它。我已尝试按照其他帖子中的建议返回一个值,但它并没有解决问题

看起来 sweetalert2 实现使用了本机 promise api,并且没有包含 polyfill。含义在某些浏览器中可能不起作用。即使 github 页面也显示相同的错误。当我在 chrome 中尝试时工作正常,但在 IE11 中不起作用。我会考虑包含一个 promise API polyfill,具体取决于您要支持的 browser/s。这与rails.

无关

Polyfill 是 here 或 google ES6 promise polyfill。将它包含在 sweetalert2 js 文件之前,你应该没问题。

如果不明白我在说什么和 promise API,好文章 here 或 google JS promise。

现在,如果所有这些都太难,and/or 令人困惑,为什么不使用原始的 sweetalert (link here),因为它不使用承诺 api。不确定他们为什么分叉,因为我看不到任何全新的功能,当然除了使用 promises 和几个新的模式图标(信息和问题)