@ types/googlemaps/index.d.ts'不是模块

AMe*_*dis 54 typescript angular-cli angular angular-cli-v6

我想在我的Angular项目中使用Google Maps API,因此我使用这两个命令来安装npm包:

npm install @agm/core --save-dev
npm install @types/googlemaps --save-dev
Run Code Online (Sandbox Code Playgroud)

我将此行添加到我的组件中:

import {} from "@types/googlemaps";
Run Code Online (Sandbox Code Playgroud)

但是我在VS Code中看到了这两个错误:

[ts] File 'h:/Angular Projects/Breakfast/client/breakfast/node_modules/@types/googlemaps/index.d.ts' is not a module.
[ts] Cannot import type declaration files. Consider importing 'googlemaps' instead of '@types/googlemaps'.
Run Code Online (Sandbox Code Playgroud)

我添加了这些行

"types": ["googlemaps"]
"moduleResolution": "node"
Run Code Online (Sandbox Code Playgroud)

到tsconfig.json和tsconfig.spec.json,但仍然没有运气.在Chrome开发工具上,我看到以下错误:

Error: Uncaught (in promise): TypeError: Cannot read property 'Autocomplete' of undefined
TypeError: Cannot read property 'Autocomplete' of undefined
Run Code Online (Sandbox Code Playgroud)

角度版本6打字稿版本2.9.2

我也尝试过Angular 5.

Cét*_*tia 106

感谢此文档链接:https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html

[Angular 6+]你只需要在你的Typescript文件的开头添加这一行(意思是第1行,之前没有任何内容):

/// <reference types="@types/googlemaps" />
Run Code Online (Sandbox Code Playgroud)

[Angular 5-]您只需要在您的Typescript文件导入中的任何位置添加此行:

import {} from "googlemaps";
Run Code Online (Sandbox Code Playgroud)

感谢下面答案,您可能还需要添加一个<root>/index.d.ts包含的文件(虽然在我的情况下不需要它):

declare module 'googlemaps';
Run Code Online (Sandbox Code Playgroud)

  • 三斜杠有效,但为什么正常的导入语句不起作用?正是这种废话让Angular很痛苦. (15认同)
  • 什么?为什么这样做?!有没有搞错?嗯...谢谢? (4认同)
  • 我应该在哪里添加这一行?在我的组件ts文件中?还是html文件? (3认同)
  • Angular 6+选项可能不是一个好主意,TS文档说:---只有当您手动创作d.ts文件时才使用这些指令.---因此不应该用于" .ts"文件,仅"d.ts"文件. (2认同)

Ste*_*aul 24

您的导入可以简化如下:

import {} from "googlemaps";
Run Code Online (Sandbox Code Playgroud)

在您指定的项目根目录中添加一个文件index.d.ts并粘贴以下内容:

declare module 'googlemaps';
Run Code Online (Sandbox Code Playgroud)


小智 8

我刚刚在我的src文件夹中创建了一个index.d.ts并添加了

声明模块'googlemaps';

它解决了这个问题


Rag*_*amy 7

工作正常

npm install --save-dev @types/googlemaps
At the beggining of your component file, type:
/// <reference types="@types/googlemaps" />
Run Code Online (Sandbox Code Playgroud)


Cam*_*est 6

对我来说,在Angular 6中,当我使用

/// <reference types="@types/googlemaps" />
Run Code Online (Sandbox Code Playgroud)


ber*_*c96 5

在我的 Angular 6+ 项目中,我已经解决了在 typescript 文件顶部使用以下行声明 googlemaps 命名空间的问题:

/// <reference path="../../../../../../node_modules/@types/googlemaps/index.d.ts"/>
Run Code Online (Sandbox Code Playgroud)

完成此操作后,您不得以其他方式导入谷歌地图,然后它就可以工作了。使用正确的 node_modules 文件夹路径。

有关 Typescript 中命名空间使用的更多信息和参考,请参阅此处的文档

  • 使用 Angular 7.2,这是本页列出的对我来说成功的解决方案。在“types”目录中尝试附加“index.d.ts”未成功,导入 {} 等也未成功。 (3认同)

小智 5

在我的Angular 7+项目中

$ npm install @types/googlemaps --save-dev
在tsconfig.app.json中

"types": [
      "googlemaps"
]
Run Code Online (Sandbox Code Playgroud)

谢谢您下面的链接 https://www.freakyjolly.com/angular-7-6-add-google-maps-in-angular-2-plus-applications-using-angular-google-maps-module-agm-core -easily /#more-2316