[为IE11添加String.repeat
polyfill,我建议使用core-js
库来填充缺少的功能。
如何为ie11的String.repeat方法添加polyfill?我没有在代码中直接使用它,可能是一些导入的库。
在IE控制台中,我收到此错误:Object doesn't support property or method 'repeat'
我也得到了'AbortController' is undefined
,但我也没有使用我的代码,可能再次使用了外部库。
我正在使用create react应用程序,并且已将其导入index.js:
import 'react-app-polyfill/ie9';
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';
我尝试将https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat#Polyfill添加到我的index.js中,但它没有执行任何操作。
其他人也有类似的问题吗?
这是针对IE的ES6不兼容问题。
使用npm添加以下polyfills
promise-polyfill
unfetch
abortcontroller-polyfill
并像这样导入它们>
import Promise from "promise-polyfill"; import fetch from 'unfetch'; import 'abortcontroller-polyfill';
对于
Abortcontroller
&对于Object.repeat
将MDN的polyfill代码复制并粘贴到您的JS文件中。可以。
编辑
对于React,它应该像这样(您可以在index.js
中完成它)>]// import polyfills
import 'react-app-polyfill/stable';
import 'react-app-polyfill/ie11';
import Promise from "promise-polyfill";
import fetch from 'unfetch';
import 'abortcontroller-polyfill';
[为IE11添加String.repeat
polyfill,我建议使用core-js
库来填充缺少的功能。
通过运行以下命令安装core-js
:
npm install --save [email protected]
在
src/index.js
内部,在最顶部导入适当的polyfill:import 'react-app-polyfill/ie11'; import 'react-app-polyfill/stable'; import 'core-js/features/string/repeat'; import React from 'react'; // ...
对于
AbortController
,通过运行以下命令进行安装:npm install --save abortcontroller-polyfill
编辑您的
src/index.js
文件以将其导入:import 'react-app-polyfill/ie11'; import 'react-app-polyfill/stable'; import 'core-js/features/string/repeat'; import 'abortcontroller-polyfill'; import React from 'react'; // ...
promise-polyfill/src/polyfill
安装unfetch/polyfill
和AbortController
,因为IE11的create-react-apps
填充物已经带有Promise
和fetch
填充物[为IE11添加String.repeat
polyfill,我建议使用core-js
库来填充缺少的功能。