【发布时间】:2020-10-04 06:43:39
【问题描述】:
错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性“自动完成” TypeError:无法读取未定义的属性“自动完成”
当我运行我的 Angular 应用程序时,这个错误不断弹出。它在第 25 行说我的 search.component.ts 文件有问题。这是我的搜索组件
/// <reference types="@types/googlemaps" />
import { Component, OnInit, ViewChild, ElementRef, NgZone } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MapsAPILoader } from '@agm/core';
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.scss'],
})
export class SearchComponent implements OnInit {
autocomplete: any;
@ViewChild('search')
public searchElementRef: ElementRef;
public searchControl: FormControl;
constructor(private zone: NgZone,
private mapsAPILoader: MapsAPILoader) { }
ngOnInit() {
this.searchControl = new FormControl();
this.mapsAPILoader.load().then(() => {
let autocomplete = new window['google'].maps.places.Autocomplete(this.searchElementRef.nativeElement, {
types: [],
componentRestrictions: {'country': 'US'}
});
autocomplete.addListener('place_changed', () => {
this.zone.run(() => {
const place: google.maps.places.PlaceResult = autocomplete.getPlace();
this.searchControl.reset();
});
});
});
}
}
如何解决这个错误?
提前致谢
【问题讨论】:
-
new window['google'].maps.places这部分是undefined或null。因此出现错误。 -
@Nikhil 我该如何解决?
-
@Rfoxes:即使出现错误,您的应用程序也能正常编译吗?
-
@Michael D 是的
-
@Rfoxes:我已经发布了答案。
标签: angular typeerror google-places-api