【问题标题】:SweetAlert2 doesn't work on IE 11, Promise is not definedSweetAlert2 在 IE 11 上不起作用,未定义 Promise
【发布时间】:2019-04-08 14:25:11
【问题描述】:

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

这个包需要一个 Promise 库,请包含一个 shim 在此浏览器中启用它(请参阅: 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)');
  }
..

如何解决?谢谢。

【问题讨论】:

  • 尝试将<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@7.1.0/dist/promise.min.js"></script> 添加到您的html。
  • 我需要使用require来添加它,而且我已经添加了bluebird polyfill。
  • 您为什么需要?您发布的文档字面意思是为 IE 添加该脚本。
  • 因为我不想在页面中添加额外的资源,我有 30 多个使用 sweetalert2 的页面/资源,我需要遍历每一个 html 并添加这个。
  • 然后把javascript放到一个文件里导出?这并不难......

标签: javascript internet-explorer promise sweetalert2


【解决方案1】:

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

window.Promise = require('bluebird');

这会将 Promise 作为窗口的全局变量加载,而不是像使用 const 那样加载文件。

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

例如:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      相关资源
      最近更新 更多