@types/googlemaps/index.d.ts' 不是模块 [英] @types/googlemaps/index.d.ts' is not a module
问题描述
我想在我的 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 Dev Tools 上,我看到以下错误:
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
Angular 版本 6打字稿版本 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 文件的开头(即第 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";
感谢下面的回答,您可能还需要添加一个文件
包含(虽然在我的情况下不需要它):
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屋!