如何在Angular 2 cli项目中添加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;
}
});
}
Run Code Online (Sandbox Code Playgroud)
我找到了另一个通过npm安装polyfill的线程(如何将Web Animations API polyfill添加到使用Angular CLI创建的Angular 2项目中 ).但如果没有npm包怎么办呢?
请注意,对于您的个人情况(使用角度2,core-js通常包含在大多数入门包中).所以你只需要将它添加到你的polyfills.ts:
import "core-js/es7";
Run Code Online (Sandbox Code Playgroud)
但是,如果您有兴趣在打字稿中声明自定义polyfill,则可以查看此问题.你只需要require("your-custom-polyfill.js")在polyfills.ts随后.
| 归档时间: |
|
| 查看次数: |
11467 次 |
| 最近记录: |