Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use window object in angular2 but vscode "Cannot find name 'window' "

I wrote an angular2 app in Visual studio code. Recently, I updated Visual Studio Code to 1.10.2. But it has highlighted window as having an error. When I checked it, I found that it says:

[ts] Cannot find name 'window'.

My code is as follows:

saveCustomIndex(customIndex:any,indexName:string){
    window.localStorage.setItem(indexName,JSON.stringify(customIndex));
}

Screenshot

Problem screenshot

How can I deal with this? Thank you!

like image 385
yilihjy Avatar asked Mar 12 '17 08:03

yilihjy


1 Answers

On tsconfig.json, make sure to have "dom" on your list of libraries.

tsconfig.json should be looking more or less like this:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

Compiler options

like image 200
zurfyx Avatar answered Oct 16 '22 15:10

zurfyx