【发布时间】:2019-01-08 14:34:19
【问题描述】:
我在我的网站中使用 Google Places API 进行自动建议,首先它工作正常,但是当我刷新特定角度组件上的页面时,它显示错误
ReferenceError: google is not defined
我搜索了许多网站,但没有一个解决方案有效我尝试在index.html 的脚本标签中添加async 和defer 但不起作用
register.component.ts
declare var google: any;
export class RegisterComponent implements AfterViewInit {
constructor() { }
ngAfterViewInit() {
const input = <HTMLInputElement>document.getElementById('googlePlaces');
const autocomplete = new google.maps.places.Autocomplete(input, { types: ['geocode'] });
google.maps.event.addListener(autocomplete, 'place_changed', () => {
this.place = autocomplete.getPlace();
})
}
}
register.component.html
<input id="googlePlaces" type="text" class="input-custom" formControlName="location" placeholder="Enter Place">
我已经在主 index.html 文件中添加了带有有效 API 密钥的脚本,这似乎没问题,因为它在第一次运行时就可以工作
【问题讨论】:
-
您在加载之前尝试使用 google。
标签: javascript angular google-maps angular6