我需要在我的应用程序中支持IE,因此我决定使用polyfill.io来支持ES6功能。 https://polyfill.io/v3/
它给了我一个URL,但是我不确定如何实际使用它。我正在使用webpack,但到目前为止我找不到解决方案。
最后一次尝试是使用ajax请求加载它
//Version 1
$.ajax({
type: 'GET',
url: "https://polyfill.io/v3/polyfill.min.js?features=String.prototype.includes",
success: Srv.main,
dataType: 'script',
}).done(() => appStart());
//Version 2
$.getScript("https://polyfill.io/v3/polyfill.min.js?features=String.prototype.includes", function (data, textStatus, jqxhr) {
appStart();
});
似乎都返回了脚本,并且实际上似乎一开始就起作用,appStart中关于String.include的操作没有错误。但是我在其他使用方法的模块中遇到了错误。所以它似乎不是全球可用的?
我希望获得一些帮助,也许有人可以通过webpack找到一个更简单的解决方案?
谢谢!
答案 0 :(得分:1)
我将使用core-js来将ECMAscript功能填充到您的应用程序中。
npm i core-js
import 'core-js/stable'; // <- at the top of your entry point
Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
[1, [2, 3], [4, [5]]].flat(2); // => [1, 2, 3, 4, 5]
Promise.resolve(32).then(x => console.log(x)); // => 32
修改
如果只想包含所需内容,则可以编辑.babelrc文件以包含“ useBuiltIns”选项
// .babelrc
{
"presets": [
["@babel/preset-env", {
"targets": [
"Last 2 versions",
]
// This option configures how @babel/preset-env handles polyfills. The 'usage' value imports
// only the specific polyfill module when they are used in each file.
"useBuiltIns": "usage"
}]
],
}