Add polyfill code in Angular 2

How to add polyfill code in Angular 2 cli-project?

For example, I would like to use the following polyfill from Mozilla MDN :

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;
    }
  });
}

I found another thread where polyfill is installed via npm ( How to add the polyfill web animation API to an Angular 2 project created using the Angular CLI ). But how to do it if there is no npm package?

+4
source share
1 answer

Please note that for your personal files (using angular 2 core-js is usually included in most starter packs). so you just need to add this to your polyfills.ts:

import "core-js/es7";

, typescript, . require("your-custom-polyfill.js") polyfills.ts, .

+4

Source: https://habr.com/ru/post/1670381/


All Articles