如何修复ReferenceError:未定义google

时间:2019-07-25 17:45:51

标签: angular typescript

我试图在某些方面使用Geocoder,但是它不再起作用,它已经起作用,但是现在出现此错误: 错误ReferenceError:未定义google

我正在使用Angular 6.0.8和@ types / googlemaps3.30.20

/// <reference types="@types/googlemaps" />
declare let google: any;
...
public geoCoder: any;
...

ngOnInit(){
    this.geoCoder = new google.maps.Geocoder;
}

,浏览器错误是这样的: 错误ReferenceError:未定义google

我觉得很奇怪,因为我已经完成了这个项目,我离开工作了,现在我回来进行一些更改不再起作用了,因此我已经离开了几天。

>

1 个答案:

答案 0 :(得分:0)

我猜是因为尚未加载Google API,并且您正在尝试访问它。

ngOnInit(){
    if(google) this.geoCoder = new google.maps.Geocoder;
}

并安装:

npm install @types/googlemaps --save-dev