SweetAlert2 不适用于 IE 11,未定义 Promise

SweetAlert2 doesn't work on IE 11, Promise is not defined

我正在使用 SweetAlert2,在 IE 11 上抛出异常:

This package requires a Promise library, please include a shim to enable it in this browser (See: https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2#1-ie-support)

因为IE 11不支持Promises,需要手动添加

我是这样使用蓝鸟的:

const Promise = require('bluebird');
const Swal = require('sweetalert2');

Swal.fire(...)
...

但是,sweetalert 的检查仍然没有通过:

..
  if (typeof Promise === 'undefined') {
    error('This package requires a Promise library, please include a shim to enable it in this browser (See: https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2#1-ie-support)');
  }
..

如何解决?谢谢

您可以使用以下方法修复它:

window.Promise = require('bluebird');

这会将 Promise 作为 window 的全局变量加载,而不是像 const.

那样加载文件

我不确定你的文件结构如何,但如果你有一个加载所有依赖项的文件,你可以简单地将上面的行添加到将在其他脚本之前调用的脚本中。

例如:

// bootstrap.js
window.Promise = require('bluebird');
window.Swal = require('sweetalert2');

// app.js
require('./bootstrap');
Swal.fire(...);

对于那些通过在页面中引用直接使用 Sweetalert2 的人:

这也可以通过在 Sweetalert2 库之后包含以下核心 js 库来解决

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