简体   繁体   中英

How to add polyfill for String.repeat function for ie11?

How would I add polyfill for String.repeat method for ie11? 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'

I also get 'AbortController' is undefined which I am also not using my code, probably external lib again.

I am using create react app and I imported in 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.

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.

Install core-js by running the following command:

npm install --save core-js@3.6.5

Inside of src/index.js , import the appropriate polyfill at the very top:

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:

npm install --save abortcontroller-polyfill

Edit your src/index.js file to import it:

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.

Add following polyfills with npm

promise-polyfill
unfetch
abortcontroller-polyfill

and import them like

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

for Abortcontroller

& For Object.repeat Copy and paste MDN's polyfill code into very first of you JS file. That will do.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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