【问题标题】:Use window object in angular2 but vscode "Cannot find name 'window' "在angular2中使用窗口对象但vscode“找不到名称'window'”
【发布时间】:2017-08-02 09:06:56
【问题描述】:

我在 Visual Studio 代码中编写了一个 angular2 应用程序。最近,我将 Visual Studio Code 更新为 1.10.2。但它已将window 突出显示为有错误。当我检查它时,我发现它说:

[ts] 找不到名称“窗口”。

我的代码如下:

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

截图

我该如何处理?谢谢!

【问题讨论】:

  • 尝试在您的 tsconfig.json 中添加选项 "lib": [ "es2015", "dom" ],

标签: javascript angular typescript visual-studio-code


【解决方案1】:

tsconfig.json 上,确保在您的库列表中包含 "dom"

tsconfig.json 应该或多或少像这样:

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

Compiler options

【讨论】:

  • 它存在,但每次我尝试构建(ng build)时我仍然有这个错误消息:“找不到名称'$window'。你的意思是'Window'吗?”
猜你喜欢
  • 2021-10-07
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 2017-01-29
  • 2020-01-10
  • 2018-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多