如何为 ie11 的 String.repeat 方法添加 polyfill?我没有在我的代码中直接使用它,它可能是一些导入的库。
在 IE 控制台中我得到这个错误:对象不支持属性或方法'repeat'
我还得到 'AbortController' is undefined
,我也没有使用我的代码,可能又是外部库。
我正在使用 create react app 并在 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 但它什么也没做。
有没有人遇到过类似的问题?
最佳答案
要为 IE11 添加 String.repeat
polyfill,我建议使用 core-js
库来 polyfill 缺失的功能。
通过运行以下命令安装core-js
:
npm install --save core-js@3.6.5
在 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';
// ...
- 自从
create- IE11 的 react-apps
polyfills 已经带有Promise
和fetch
polyfills - 但是,如果您没有使用
create-react-app
IE11 polyfill,我建议您查看以下答案:
关于javascript - 如何为 ie11 的 String.repeat 函数添加 polyfill?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60453044/