在Angular 2中添加多填充代码



我如何在Angular 2 Cli-project中添加polyfill的代码?

例如,我想使用Mozilla Mdn的以下polyfill:

if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(searchElement, fromIndex) {
      if (this == null) { throw new TypeError('"this" is null or not defined'); }
      var o = Object(this);
      var len = o.length >>> 0;
      if (len === 0) { return false; }
      var n = fromIndex | 0;
      var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
      while (k < len) {
        if (o[k] === searchElement) { return true; }
        k++;
      }
      return false;
    }
  });
}

我找到了另一个线程,其中通过NPM安装了多填充(如何将Web Animations API Polyfill添加到使用Angular CLI创建的Angular 2项目)。但是,如果没有NPM软件包,该怎么办?

请注意,对于您的人体情况(使用Angular 2,通常包含大多数启动器包中的core-js)。因此,您只需要将其添加到polyfills.ts

import "core-js/es7";

但是,如果您有兴趣在打字稿中宣布自定义polyfills,则可以查看此问题。然后,您只需要在polyfills.ts中进行require("your-custom-polyfill.js")

最新更新