如何为ie11的String.repeat函数添加polyfill?

问题描述 投票:2回答:2

如何为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中,但它没有执行任何操作。

其他人也有类似的问题吗?

javascript reactjs internet-explorer-11 create-react-app polyfills
2个回答
0
投票

这是针对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/polyfillAbortController,因为IE11的create-react-apps填充物已经带有Promisefetch填充物

0
投票

[为IE11添加String.repeat polyfill,我建议使用core-js库来填充缺少的功能。

© www.soinside.com 2019 - 2024. All rights reserved.