简体   繁体   English

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

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

I'm using SweetAlert2 and on IE 11 throws exception:我正在使用 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 )这个包需要一个 Promise 库,请在这个浏览器中包含一个 shim 来启用它(参见: https : //github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2#1-ie-support

Because IE 11 doesn't support Promises and needs to be added manually.因为IE 11不支持Promises,需要手动添加。

I'm using bluebird like so:我像这样使用蓝鸟:

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

Swal.fire(...)
...

But still, sweetalert's check doesn't pass:但是,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)');
  }
..

How to fix it?如何解决? Thanks.谢谢。

You can fix it with the following:您可以使用以下方法修复它:

window.Promise = require('bluebird');

This will load Promise as a global variable of your window instead of the file like you did with the const .这会将 Promise 作为窗口的全局变量加载,而不是像使用const那样加载文件。

I'm not sure how your file structure is, but if you have a file that loads all the dependencies, you can simply add the line above to the script that will be called before your other scripts.我不确定您的文件结构如何,但是如果您有一个加载所有依赖项的文件,您只需将上面的行添加到将在其他脚本之前调用的脚本中。

For example:例如:

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

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

For those who are using Sweetalert2 directly by referencing it in pages:对于那些通过在页面中引用它直接使用 Sweetalert2 的人:

This could also be fixed by including the below core js lib after Sweetalert2 lib这也可以通过在 Sweetalert2 库之后包含以下核心 js 库来解决

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM