2017-04-26 22 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

相關問題