YD_*_*YD_ 13 google-maps typescript ionic2 typescript-typings angular
我在Ionic 2视频教程中关注Joshua Morony的Google Maps入门.我想在我的应用程序中使用谷歌地图,我最终得到一个打字稿错误.这是 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);}
Run Code Online (Sandbox Code Playgroud)
我试过了npm install --save @types/googlemaps
,
但它仍然给我相同的打字稿错误Typescript错误找不到名字'谷歌'
Bir*_*chi 17
我通过安装解决了它:
$npm install @types/googlemaps --save-dev
Run Code Online (Sandbox Code Playgroud)
Dou*_*bin 10
要扩展@suraj的答案,您应该:
declare var google;
Run Code Online (Sandbox Code Playgroud)
你正试图在课堂之外使用它.
就像在Josh Morony视频中一样,我把它放在导入之下但是在类声明和注释之前(@Injectable()
等等).如果你把它放在导入之上或者在类的末尾之下(并且仍然在类之外),如果你因为某种原因如此倾向,我认为它仍然可以工作.
npm install --save-dev @types/googlemaps
import {} from '@types/googlemaps';
Run Code Online (Sandbox Code Playgroud)
从这个答案
要么
在你component.ts
的页面文件中声明它是这样的
declare var google;
之前 export class component {}
您必须安装以下类型:
npm install --save-dev @types/googlemaps
Run Code Online (Sandbox Code Playgroud)
并在您的Typescript文件中使用命名空间“ google”:
[Angular 6+]在开头添加以下行:
/// <reference types="@types/googlemaps" />
Run Code Online (Sandbox Code Playgroud)
[Angular 5-]添加此行:
import {} from '@types/googlemaps';
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17381 次 |
最近记录: |