簡體   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