SweetAlert2:html 的意外类型!预期 "string",得到对象

SweetAlert2: Unexpected type of html! Expected "string", got object

我正在尝试让 SweetAlert2 对话框显示两个按钮,代码片段如下:

swal('Some text', {
  buttons: {
    buttonA: {
      text: 'Some text',
      value: 'buttonA',
      className: 'some-class'
    },
    buttonB: {
      text: 'Some other text',
      value: 'buttonB',
      className: 'some-class'
    }
  },
  buttonsStyling: false
}).then((result) => {
  switch (result) {
    case 'buttonA':
      // Do something
      break;
    default:
      // Do something else
  }
})

但我收到以下错误消息:

SweetAlert2: Unexpected type of html! Expected "string", got object

此外,对话框只显示一个 "OK" 按钮

我做错了什么?

使用最新的sweetalert

只需将您的 sweetalert 脚本文件替换为:

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

here 是文档

您已安装 SweetAlert2 but using the API of SweetAlert。这两个是现在具有不同 API 的不同插件。

使用 SweetAlert2 的 API 得到想要的结果:

swal({
  title: 'Hello world!',
  confirmButtonText: 'Some text',
  cancelButtonText: 'Some other text',
  confirmButtonClass: 'some-class',
  cancelButtonClass: 'some-other-class',
  showCancelButton: true
}).then(function(result) {
  if (result.value) {
    console.log('button A pressed')
  } else {
    console.log('button B pressed')
  }
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@7"></script>