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

时间:2018-04-18 10:09:03

标签: angular typescript internet-explorer-11

我在IE 11中遇到此错误,在所有其他常见浏览器上它工作正常。

我使用此代码使用'includes':

const keys = Object.keys(this.service.content);

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

任何替代方案?

4 个答案:

答案 0 :(得分:3)

我遇到了 Angular 7.0 + IE11

我通过在polyfill.ts文件中添加一行来获得解决方案。

import 'core-js/es7/array';

我从本文Internet Explorer 11 and Angular 2+

中获得了此解决方案

注意::如果您想避免 IE 浏览器出现这种情况,那么应该感谢Maarten Merken阅读本文。

答案 1 :(得分:2)

如果您使用的是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所需的其他polyfill。

答案 2 :(得分:1)

您应该使用针对Array.prototype.includes或旧好Array.prototype.indexOf

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

答案 3 :(得分:1)

如果有人正在寻找ReactJS解决方案-

import 'core-js/es6/string';的开头使用index.js解决了我的问题。

我还使用import 'react-app-polyfill/ie11';支持在IE11中运行React。

  

react-app-polyfill

     

此包装包括用于各种包装的填充料   浏览器。它包括最低要求和常用语言   Create React App项目使用的功能。

https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md