開発覚書はてな版

個人的な開発関連の備忘録

【Angular】IE11でArray.includesを使用する

概要

Angular や TypeScript で Array.includes を使用した場合、IE11ではundefinedでエラーになります。 IE11はES5を採用しているため、ES7の Array.includes は対応していない為です。

そのため、IE11でも使いたい場合はpolyfillsの設定が必要になります。 Angularの標準 polyfills.ts ではES7のものは記載されていないため、個別に追加する必要があります。

使用ライブラリ

  • Angular 7.0.x

変更箇所

  • polyfills.tsimport "core-js/es7/array"; を追加する。

サンプルソース

polyfills.ts
// 省略

/** 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";
import "core-js/es7/array"; // 追加

// 省略

メモ

  • core-js/es7/array にはES7で追加されたもののみ対応されている。そのため、core-js/es6/arrayコメントアウトするとES6のファンクションが使えなくなるため注意。