公司前端项目用的是Angular6,最近在项目中用到了es7中数组的新特性includes,在谷歌浏览器正常运行,在IE浏览器运行的时候报错,识别不了includes。一开始的想法就是用indexOf代替一下,转念一想,是否可以做一个兼容,于是自己写了个polyfills,兼容如下:
同时支持String和Array:
// 兼容es7 includes string Array
(function(types) {
types.forEach(function(type) {
if (!type.prototype.includes) {
type.prototype.includes = function(search, start) {
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
} else {
return this.indexOf(search, start) !== -1;
}
};
}
});
})([String, Array]);
在angular-cli.json中引入就可以了。

针对Angular6项目中使用ES7的includes方法在IE浏览器上出现的兼容性问题,本文详细介绍了如何通过自定义polyfills来解决此问题,确保在不同浏览器下都能正常运行。

530

被折叠的 条评论
为什么被折叠?



