为 .includes 添加了 poly-fill,但尽管在所有其他区域都已解决,但仍然在一个区域出现错误

Added poly-fill for .includes but still getting the error in one area despite being resolved in all other areas

IE 11 触发 Object doesn't support property or method 'includes' 因为它在 IE11 中不受支持:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Browser_compatibility

您必须添加以下 polyfill 才能工作:

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}

我已将它添加到我的 index.jsx 中,它解决了任何出现 .includes() 的问题,但我不确定为什么。

我在 React 容器中有这段 JS:

removeInfectedFiles() {
    let { filesAccepted } = this.state;
    const { infected } = this.props.upload.data;

    this.setState({
        ...this.state,
        filesAccepted: filesAccepted.filter(
            file => !infected.includes(file.key)
        )
    })

    var filesInfected = [];

    _.map(infected, i => {
        filesInfected.unshift(
            <p key={ i }>{ i }</p>
        )
    });

    this.setState({
        filesInfected
    })

}

适用于除 IE 11 以外的所有其他浏览器。

在将文件写入服务器之前,会对其进行病毒扫描。如果一个文件有服务器响应一个被感染的文件列表,这应该是 `this.props.upload.data... 并且显然不会将它们写入服务器。这将从成功提交的文件列表中删除文件名。

Array.prototype.includes()String.prototype.includes() 不同。如果 infected 是数组,则需要为数组方法包含一个 polyfill。