【问题标题】:Nextjs 'endsWith' polyfill problem on edge and ie11边缘和 ie11 上的 Nextjs 'endsWith' polyfill 问题
【发布时间】:2020-11-24 20:10:03
【问题描述】:

由于Object doesn't support property or method 'endsWith' 错误,我的网站中断并无法运行。我试过用import 'core-js/features/string/ends-with' 或

填充它
if (!String.prototype.endsWith) {
  String.prototype.endsWith = function (search, this_len) {
    if (this_len === undefined || this_len > this.length) {
      this_len = this.length
    }
    return this.substring(this_len - search.length, this_len) === search
  }
}

我已经在我的自定义_app 文件中做到了这一点,但它在 edge 和 ie11 中仍然失败。我错过了什么?

编辑:根据https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith#Browser_compatibility 判断,Edge 应该支持endsWith

【问题讨论】:

  • 当您在 Next.js 项目中使用外部 npm 模块时,您必须手动为它们添加 polyfill。添加polyfills后,您可以参考this post关于IE 11中Next Js的解决方案。

标签: reactjs next.js microsoft-edge polyfills


【解决方案1】:

尝试将此添加到您的代码中。

String.prototype.endsWith = function(pattern) {
  var d = this.length - pattern.length;
  return d >= 0 && this.lastIndexOf(pattern) === d;
};

【讨论】:

  • 我不在任何地方使用endsWith,可能是一些依赖。正如我所说,我在 _app.js 中包含了 polyfill
  • 正如我所说,我在 next.js 应用程序的主入口文件中这样做了,但它仍然抛出错误
猜你喜欢
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2021-01-09
  • 2015-04-17
相关资源
最近更新 更多