【问题标题】:Visual Studio Code: node_modules/@types/googlemaps/index.d.ts' is not a moduleVisual Studio 代码:node_modules/@types/googlemaps/index.d.ts' 不是模块
【发布时间】:2018-12-05 14:53:36
【问题描述】:

我遵循了这个教程: Integrating Google Maps in Angular 5 在我的 Angular 5 应用程序中使用 Visual Studio Code(Mac 上的版本 1.24.1)开始使用 Google 地图。直到最近一切正常,但从几天前开始(我的猜测是 VS Code 的更新发生在后台)我在 Visual Studio Code 中导入 Google Maps 时遇到错误,之前从未给过我错误:

import { } from '@types/googlemaps';

我收到以下错误:

File 'full_path/node_modules/@types/googlemaps/index.d.ts' is not a module.

(注意 full_path 是完整的父路径,为简洁起见此处不显示)

我可以看到该库位于我的 node_modules 目录中的正确位置。在我的 package.json(版本 3.30.10)中也正确引用了该库。我没有运行 npm update 或在它工作和开始给出错误之间的任何其他更新。

当我运行“npm start”或“npm run build”时,它工作得很好,应用程序像以前一样得到服务(谷歌地图完美呈现),所以这似乎是一个 Visual Studio Code 问题。

从这个意义上说,它并没有真正阻止我,但是从我的 IDE 中排除错误会很好。有没有人对如何摆脱错误有任何指示?谢谢!

编辑 我在这里读到:How to install TS typings for Google maps 有两种可能的解决方案。三斜杠指令或更改您的 tsconfig.json。对我来说,只有三斜杠指令有效:

/// <reference path="<relevant path>/node_modules/@types/googlemaps/index.d.ts" />

我仍然会对可以导入 Google 地图类型的真正修复感兴趣。

【问题讨论】:

  • 你试过import '@types/googlemaps'吗?
  • 刚刚尝试过,但是在运行 npm run build 时出现以下错误:找不到模块:错误:无法解析“”中的“@types/googlemaps”

标签: angular typescript visual-studio-code


【解决方案1】:

仅使用 .component.ts 文件顶部的下一行,它对我有用。

/// <reference types="@types/googlemaps" />

我的 tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

我的@types/googlemaps 版本 - "@types/googlemaps": "^3.30.11",

我的@angular/core 版本 - "@angular/core": "^6.1.0",

不要使用下面的行(一些答案推荐其中任何一个)早些时候,当我使用 angular 6.0.0 时,它可以使用这些,但是当我升级时它没有:-

import { } from 'googlemaps'; 
import { } from '@types/googlemaps';

【讨论】:

    猜你喜欢
    • 2018-12-07
    • 2022-12-16
    • 2020-01-18
    • 2019-04-18
    • 2017-02-07
    • 2017-11-09
    • 2019-08-31
    • 2018-11-10
    • 2017-07-08
    相关资源
    最近更新 更多