我在Ionic 2视频教程中关注Joshua Morony的谷歌地图入门。 我想在我的应用程序中使用谷歌地图,我最终得到一个打字稿错误。这是其中的一部分 pages / home / home.ts文件
initMap(){
let latLng= new google.maps.LatLng(6.929848, 79.857407);
let mapOpt={
center : latLng,
zoom : 15,
mapTypeId :google.maps.MapTypeId.ROADMAP
};
this.map= new google.maps.Map(this.mapElement.nativeElement,mapOpt);}
我尝试了npm install --save @types/googlemaps
,
但它仍然给我相同的打字稿错误打字稿错误 找不到名称' google'
答案 0 :(得分:15)
我通过安装解决了它:
$npm install @types/googlemaps --save-dev
答案 1 :(得分:9)
要扩展@suraj的答案,您应该:
declare var google;
你正试图在课堂之外使用它。
就像在Josh Morony视频中一样,我把它放在导入之下但是在类声明和注释(@Injectable()
之后)之前。如果你把它放在导入之上或者在类的末尾(并且仍然在类之外),如果你因为某种原因如此倾向,我认为它仍然可以工作。
答案 2 :(得分:9)
npm install --save-dev @types/googlemaps
import {} from '@types/googlemaps';
或强>
在您网页的component.ts
文件中声明如下
declare var google;
之前 export class component {}
答案 3 :(得分:6)
您必须安装以下类型:
npm install --save-dev @types/googlemaps
并在您的Typescript文件中使用命名空间“ google”:
[Angular 6+]在开头添加此行:
/// <reference types="@types/googlemaps" />
[Angular 5-]添加此行:
import {} from '@types/googlemaps';
答案 4 :(得分:0)
无需做其他事情,只需进入 index.d.ts 并粘贴此,然后再声明名称空间google.maps
declare module 'googlemaps';