在Angular 2中添加polyfill代码 [英] Add polyfill code in Angular 2
问题描述
我如何在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;
}
});
}
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?
推荐答案
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屋!