类型错误:无法读取未定义的属性“自动完成”

Rfo*_*xes 4 typeerror google-places-api angular

错误错误:未捕获(承诺中):类型错误:无法读取未定义的属性“自动完成”类型错误:无法读取未定义的属性“自动完成”

当我运行 Angular 应用程序时,此错误不断弹出。它说我的 search.component.ts 文件第 25 行有问题。这是我的搜索组件

    /// <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();
        });
      });
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

我该如何修复这个错误?

提前致谢

Zed*_*haw 8

检查您是否已激活并导入&libraries=places脚本嵌入链接

<script src="https://maps.googleapis.com/maps/api/js?key={{ config.MAPS_API_KEY }}&libraries=places&callback=initMap&v=weekly" async></script>
Run Code Online (Sandbox Code Playgroud)