Angular .includes在类型“string[]”上不存在
试图在我的component.ts文件中使用.includes,但它引发了以下错误。帮我解决这个问题 错误 类型“string[]”上不存在属性“includes” app.component.tsAngular .includes在类型“string[]”上不存在,angular,typescript,Angular,Typescript,试图在我的component.ts文件中使用.includes,但它引发了以下错误。帮我解决这个问题 错误 类型“string[]”上不存在属性“includes” app.component.ts permissionCookie: string[]; this.permissionCookie = cookieService.get("permissions").split(","); this.permissionFlag = this.permissionCookie.includes
permissionCookie: string[];
this.permissionCookie = cookieService.get("permissions").split(",");
this.permissionFlag = this.permissionCookie.includes("22");
ts.config
{
"compilerOptions": {
"baseUrl": "",
"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"
]
}
}
这也帮不了我
试着把它声明为
private permissionCookie:Array<string>;
constructor() {
let a = this.permissionCookie.includes("22");
console.log(a);
}
将ES2017添加到tsconfig.json中的lib数组:
这应该在TypeScript 2.1+中工作
A.this.permissionCookie-这是一个数组吗?includes仅适用于数组。您说过您发布的链接中的解决方案不起作用,但您的配置根本不使用它。它说使用es2016作为目标,而你不是。当Include是ES2016的一部分时,其他所有内容都设置为ES6。是的,它是@RemyaJ@GillesC我的意思是,我尝试将目标更改为es2016,虽然它不起作用,但在拆分和显示后,您可以安慰您的this.permissionCookie吗?[32,33,21,22]不确定,但嘿,这对我来说很有效`lib:[es7,dom]`在我的ts.config文件now.includes.splice中改成了这个,一切正常。感谢@RemyaJcan u post ur config.ts文件内容的支持@remyajhan已经添加了它,您可以查看。
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
// "moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
// "noImplicitAny": true,
// "suppressImplicitAnyIndexErrors": true,
"types": ["jquery"]
},
"exclude": [
"node_modules/*",
"**/*-aot.ts"
]
}
{
"compilerOptions": {
...
"lib": ["dom", "es2017"],
...
}
}