Javascript 未捕获类型错误:无法读取属性';然后';使用Sweet-Alert的未定义属性

Javascript 未捕获类型错误:无法读取属性';然后';使用Sweet-Alert的未定义属性,javascript,sweetalert,Javascript,Sweetalert,我正在使用,我得到以下错误 未捕获的TypeError:无法读取未定义的属性“then” 当我使用与SweetAlert页面中建议的完全相同的代码时 swal({ title: 'Are you sure?', text: "You won't be able to revert this!", type: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor:

我正在使用,我得到以下错误

未捕获的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
  }
})

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

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

Polyfill是或谷歌ES6承诺Polyfill。在SweetAlert2JS文件之前包含它,您应该会没事的

如果我不明白promise API、关于它的好文章或google JS promise在说什么


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

您添加了对相关js文件的引用吗?因为这意味着上下文中没有swal。因此,这意味着要么javascript文件未在文件中引用,要么swal未正确创建。@HiradNikoo我已引用了swal。它实际上会弹出模态视图。但是在确认/取消之后,它没有通过。是的,OJay提到的承诺肯定有问题,但请尝试我的解决方案,看看它是否有效。对不起,我认为OJay的回答是正确的。请从这里下载承诺是的,我想你是对的。我看到了密码。它在第321行使用本机Promise。使用原始sweetalert代码解决了问题。谢谢