@ types / googlemaps / index.d.ts'不是一个模块 [英] @types/googlemaps/index.d.ts' is not a module

查看:294
本文介绍了@ types / googlemaps / index.d.ts'不是一个模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Angular项目中使用Google Maps API,因此我使用了以下两个命令来安装npm软件包:

I want to use the Google Maps API with my Angular project, so I used these two commands to install npm packages:

npm install @agm/core --save-dev
npm install @types/googlemaps --save-dev

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

I added this line to my component:

import {} from "@types/googlemaps";

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

But I see these 2 errors in 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'.

我添加了以下几行

"types": ["googlemaps"]
"moduleResolution": "node"

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

to tsconfig.json and tsconfig.spec.json, but still no luck. On Chrome Dev Tools, I see the below error:

Error: Uncaught (in promise): TypeError: Cannot read property 'Autocomplete' of undefined
TypeError: Cannot read property 'Autocomplete' of undefined

角度版本6
Typescript版本2.9.2

Angular version 6 Typescript Version 2.9.2

我也在Angular 5中尝试过。

I tried from Angular 5, too.

推荐答案

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

[Angular 6+]您只需在<您的Typescript文件的 strong>开头(意为第1行,之前没有任何内容):

[Angular 6+] You only have to add this line at the beginning (meaning line 1, with nothing before) of your Typescript file :

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

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

[Angular 5-] You only have to add this line anywhere in your Typescript file imports :

import {} from "googlemaps";

感谢在下面的答案中,您可能还需要添加文件< root> /index.d.ts ,其中包含(尽管在我情况):

Thanks to the answer below, you may also need to add a file <root>/index.d.ts containing (didn't need it though in my case) :

declare module 'googlemaps';

这篇关于@ types / googlemaps / index.d.ts'不是一个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆