未捕获的TypeError:无法使用Sweet Alert读取未定义的属性'then'

mar*_*maf 8 javascript sweetalert

我正在使用SweetAlert2,我收到以下错误

未捕获的TypeError:无法读取未定义的属性'then'

当我使用与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
  }
})
Run Code Online (Sandbox Code Playgroud)

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

OJa*_*Jay 9

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

Polyfill在这里或谷歌ES6承诺polyfill.在sweetalert2 js文件之前包含它,你应该没问题.

如果不理解我正在谈论的诺言API,这里有关于它的好文章,或谷歌JS承诺.

现在,如果所有这些都太难和/或令人困惑,为什么不使用原始的sweetalert(链接在这里),因为它不使用promise api.不完全确定为什么他们分叉,因为除了当然使用承诺,还有一些新的模态图标(信息和问题),我看不到任何大的新功能