Internet Explorer 11 - 对象不支持属性或方法“includes”

6
我在IE 11中遇到了这个错误,在其他常用浏览器上它能正常工作。我使用的代码使用了“includes”函数:
```javascript // code block ```
const keys = Object.keys(this.service.content);

            if (keys.includes(splitUrl[splitUrl.length - 1])) {
                this.router.navigateByUrl(`/mysite/${splitUrl[splitUrl.length - 1]}/1`);
            }

有其他替代方案吗?
4个回答

6

我曾遇到与Angular 7.0+IE11相关的问题。

通过在我的polyfill.ts文件中添加一行代码,我找到了解决方案。

import 'core-js/es7/array';

我从这篇文章 Internet Explorer 11 and Angular 2+ 中得到了这个解决方案。

NB:如果你想避免IE浏览器出现这种情况,那么你应该阅读这篇文章,感谢Maarten Merken


5

3
如果您正在使用 angular/cli,打开 polyfills.ts 文件并取消注释所需的 polyfill。
/** IE9, IE10 and IE11 requires all of the following polyfills. **/
// import 'core-js/es6/symbol';
// import 'core-js/es6/object';
// import 'core-js/es6/function';
// import 'core-js/es6/parse-int';
// import 'core-js/es6/parse-float';
// import 'core-js/es6/number';
// import 'core-js/es6/math';
// import 'core-js/es6/string';
// import 'core-js/es6/date';
import 'core-js/es6/array';
// import 'core-js/es6/regexp';
// import 'core-js/es6/map';
// import 'core-js/es6/weak-map';
// import 'core-js/es6/set';

您可能还需要包括 IE11 需要的其他填充程序。


2

您应该使用Array.prototype.includes的polyfill或旧好的Array.prototype.indexOf

 if (keys.indexOf(splitUrl[splitUrl.length - 1]) !== -1) {
   this.router.navigateByUrl(`/mysite/${splitUrl[splitUrl.length - 1]}/1`);
 }

嗯,indexOf 不知道为什么给我的结果不一样...但它肯定帮助我朝着正确的方向前进。 - bobdolan
@bobdolan 你所说的“相同结果”是什么意思?能否提供一个例子? - uladzimir
我在代码中正在做的是将一个对象(类型:字符串)与分割后的URL进行比较。如果分割后的URL包含该字符串,则将用户重定向到另一个部分。 - bobdolan
1
谢谢,indexOf 像魔法一样好用。但有点可惜的是,为了支持过时的浏览器,我们需要花费时间和精力… - Yuri Kiriachek
1
@YuriKiriachek 常规建议是先支持IE,其他浏览器都会正常工作的 ;) - uladzimir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接