简体   繁体   English

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

[英]How to add polyfill for String.repeat function for ie11?

How would I add polyfill for String.repeat method for ie11?我将如何为 ie11 的 String.repeat 方法添加 polyfill? I am not using it directly in my code, it's probably some of the imported libs.我没有直接在我的代码中使用它,它可能是一些导入的库。

In IE console I get this error: Object doesn't support property or method 'repeat'在 IE 控制台中,我收到此错误: Object doesn't support property or method 'repeat'

I also get 'AbortController' is undefined which I am also not using my code, probably external lib again.我也得到'AbortController' is undefined ,我也没有使用我的代码,可能再次使用外部库。

I am using create react app and I imported in index.js:我正在使用 create react app 并在 index.js 中导入:

import 'react-app-polyfill/ie9';
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';

I tried adding https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat#Polyfill to my index.js but it didn't do anything.我尝试将https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat#Polyfill添加到我的 index.js 但它没有做任何事情。

Did anyone else have similar problem?有没有其他人有类似的问题?

To add a String.repeat polyfill for IE11, I suggest using the core-js library to polyfill missing features.要为 IE11 添加String.repeat polyfill,我建议使用core-js库来填充缺失的功能。

Install core-js by running the following command:通过运行以下命令安装core-js

npm install --save core-js@3.6.5

Inside of src/index.js , import the appropriate polyfill at the very top:src/index.js ,在最顶部导入适当的 polyfill:

import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';
import 'core-js/features/string/repeat';

import React from 'react';
// ...

As for AbortController , install it by running the following command:至于AbortController ,请通过运行以下命令来安装它:

npm install --save abortcontroller-polyfill

Edit your src/index.js file to import it:编辑您的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';
// ...

This is for IE's ES6 incompatibility issues.这是针对 IE 的 ES6 不兼容问题。

Add following polyfills with npm使用 npm 添加以下 polyfill

promise-polyfill
unfetch
abortcontroller-polyfill

and import them like并像这样导入它们

import Promise from "promise-polyfill";
import fetch from 'unfetch';
import 'abortcontroller-polyfill';

for Abortcontroller用于Abortcontroller

& For Object.repeat Copy and paste MDN's polyfill code into very first of you JS file. & For Object.repeat将 MDN 的Object.repeat代码复制并粘贴到您的第一个 JS 文件中。 That will do.那就行了。

EDIT For React this should look like this (you can do it in index.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';

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

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