我正在使用角度cli。
我真的不明白我是否可以在我的 Angular html 模板代码中使用 array.prototype.includes。
如果我尝试在我的 .ts 代码中使用 array.prototype.includes,打字稿编译器会抛出错误消息
Property 'includes' does not exist on type 'Answer[]'
从以下链接 https://github.com/AngularClass/angular2-webpack-starter/issues/931看来,我必须修改 tsconfig.json 文件以将 es7 指定为 lib,而不是 es6。这适用于 .ts 文件。
但是,无论如何能够在我的 Angular 2 html 模板中使用 array.prototype.includes 并将其转换为等效的 es5 吗?
<span [class.incorrect]="displayAnswer && !answer.correct && userAnswers.includes(answer)">Answer 2</span>
我知道 .includes 调用保持原样,因为当我在 IE 11 上运行应用程序时它们会引发错误。
Object doesn't support property or method 'includes'
还是我需要使用 polyfill 之类的?
这是我的 tsconfig.json 文件
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es6", "dom"],
"mapRoot": "./",
"module": "es6",
"moduleResolution": "node",
"outDir": "../dist/out-tsc",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
]
}
}
谢谢