简体   繁体   中英

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

问题截图

How can I deal with this? Thank you!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM