在Angular 2中添加polyfill代码 [英] Add polyfill code in Angular 2

查看:231
本文介绍了在Angular 2中添加polyfill代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在Angular 2 cli项目中添加polyfill的代码?

How would I add code of a polyfill in a Angular 2 cli-project?

例如,我想从 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;
    }
  });
}

我发现了另一个通过npm安装polyfill的线程(

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

推荐答案

请注意,对于您的个人情况(使用角度2,

Note that for your personnal case (using angular 2, core-js is normally included in most starter packs). so you just need to add this to your polyfills.ts:

import "core-js/es7";

但是,如果您有兴趣在打字稿中声明自定义polyfill,可以查看.

However if you are interested in declaring custom polyfills in typescript, you can take a look at this question. You'll just need to require("your-custom-polyfill.js") in polyfills.ts then.

这篇关于在Angular 2中添加polyfill代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆