2017-04-26 1126 views
0

尝试在我的component.ts文件中使用.includes,但它会引发以下错误。帮助我解决问题。.includes在类型'string []'上不存在

  • 错误

属性 '包括' 不上型存在 '串[]'

  • app.component.ts

    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" 
        ] 
        } 
    } 
    

    Property 'includes' does not exist on type 'string[]'没有帮助我要么

+0

this.permissionCookie - 这是一个数组吗?包含仅适用于数组。 – RemyaJ

+0

你说你发布的链接中的解决方案没有工作,但你的配置根本不使用它。它说为目标使用'es2016',而你不是。 'includes'是ES2016的一部分时,其他所有内容都设置为ES6。 – GillesC

+0

是的,这是... @ RemyaJ –

回答

0

尝试声明为,

private permissionCookie:Array<string>; 
constructor() { 
    let a = this.permissionCookie.includes("22"); 
    console.log(a); 
} 

tsconfig.json - >

{ 
    "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" 
    ] 
} 
+0

:(没有..没有工作 –

+0

你可以控制你的this.permissionCookie后分裂并显示? – RemyaJ

+0

[“32”,“33”,“21”,“22”] –

-1

添加"ES2017"到你的"lib"数组中tsconfig.json

{ 
    "compilerOptions": { 
    ... 
    "lib": ["dom", "es2017"], 
    ... 
    } 
} 

这应该在2.1以上版本的打字稿工作。

A related issue

相关问题