使用react polyfills但出现错误对象不支持IE11中的属性或方法“重复”

IT技术 reactjs internet-explorer-11 polyfills
2021-05-13 03:04:51

我正在使用 react polyfills 并且该站点在 IE11 中工作,除非使用

   changeHeight() {
     let height = 0;
       for (let child of this.Element.current.children) {
       height = Math.max(height, child.clientHeight);
       }
    this.Element.current.style.height = `${height}px`;
    }

IE 中的错误是 对象不支持属性或方法“重复”

我已经在 package.json 中定义了浏览器并导入了以下内容

  • 导入'react-app-polyfill/ie11';
  • 导入'react-app-polyfill/stable'

但页面不会呈现。

我也收到错误

SCRIPT5022:抛出异常但未捕获

main.chunk.js (6068,465)

1个回答

IE 11不支持新的 ECMAScript 2015repeat()方法。您可以添加以下polyfill来修复它:

if (!String.prototype.repeat) {
  String.prototype.repeat = function(count) {
    'use strict';
    if (this == null)
      throw new TypeError('can\'t convert ' + this + ' to object');

    var str = '' + this;
    // To convert string to integer.
    count = +count;
    // Check NaN
    if (count != count)
      count = 0;

    if (count < 0)
      throw new RangeError('repeat count must be non-negative');

    if (count == Infinity)
      throw new RangeError('repeat count must be less than infinity');

    count = Math.floor(count);
    if (str.length == 0 || count == 0)
      return '';

    // Ensuring count is a 31-bit integer allows us to heavily optimize the
    // main part. But anyway, most current (August 2014) browsers can't handle
    // strings 1 << 28 chars or longer, so:
    if (str.length * count >= 1 << 28)
      throw new RangeError('repeat count must not overflow maximum string size');

    var maxCount = str.length * count;
    count = Math.floor(Math.log(count) / Math.log(2));
    while (count) {
       str += str;
       count--;
    }
    str += str.substring(0, maxCount - str.length);
    return str;
  }
}