繁体   English   中英

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

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

我将如何为 ie11 的 String.repeat 方法添加 polyfill? 我没有直接在我的代码中使用它,它可能是一些导入的库。

在 IE 控制台中,我收到此错误: Object doesn't support property or method '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库来填充缺失的功能。

通过运行以下命令安装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';
// ...

这是针对 IE 的 ES6 不兼容问题。

使用 npm 添加以下 polyfill

promise-polyfill
unfetch
abortcontroller-polyfill

并像这样导入它们

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

用于Abortcontroller

& For Object.repeat将 MDN 的Object.repeat代码复制并粘贴到您的第一个 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