ไม่พบชื่อ 'google' เชิงมุม 8

เนื่องจากมันแสดงข้อผิดพลาดระหว่างการบิลด์

ERROR in app/pages/TestPage4/TestPage4.ts:27:27 - error TS2304: Cannot find name 'google'.

27 this.geoCoder = google.maps.Geocoder ใหม่; ~~~~~~ app/pages/TestPage4/TestPage4.ts:29:32 - ข้อผิดพลาด TS2304: ไม่พบชื่อ 'google'

29 const เติมข้อความอัตโนมัติ = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement); ~~~~~~ app/pages/TestPage4/TestPage4.ts:33:24 - ข้อผิดพลาด TS2503: ไม่พบเนมสเปซ 'google'

33 สถานที่ const: google.maps.places.PlaceResult = autocomplete.getPlace(); ~~~~~~

นี่คือรหัสของฉันด้านล่าง

import { Component, OnInit, ElementRef, ViewChild, NgZone } from '@angular/core';
import { MapsAPILoader } from '@agm/core';

@Component({
  selector: 'app',
  templateUrl: './TestPage4.html',
  styleUrls: ['./TestPage4.css']
})

export class TestPage4 implements OnInit {
  lat: number;
  lng: number;
  zoom = 1;
  private geoCoder;

  @ViewChild('search', { static: false})
  public searchElementRef: ElementRef;

  constructor(
    private mapsAPILoader: MapsAPILoader,
    private ngZone: NgZone) {}

  ngOnInit() {
    //load Places Autocomplete
    
    this.mapsAPILoader.load().then(() => { 
      this.geoCoder = new google.maps.Geocoder;

      const autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement);
      autocomplete.addListener("place_changed", () => {
        this.ngZone.run(() => {
          //get the place result
          const place: google.maps.places.PlaceResult = autocomplete.getPlace();
          console.log(place);
          //verify result
          if (place.geometry === undefined || place.geometry === null) {
            return;
          }

          for (var i = 0; i < place.address_components.length; i++) {
            let addressType = place.address_components[i].types[0];
            //if (componentForm[addressType]) {
            //  var val = place.address_components[i][componentForm[addressType]];
            //  document.getElementById(addressType).value = val;
            //}
            // for the country, get the country code (the "short name") also
            console.log(addressType);
            if (addressType == "country") {
              console.log(place.address_components[i].short_name);
              console.log(place.address_components[i].long_name);
            }
            else{
              console.log('---others---');
              console.log(place.address_components[i].short_name);
              console.log(place.address_components[i].long_name);
            }
          }

          //set latitude, longitude and zoom
          this.lat = place.geometry.location.lat();
          this.lng = place.geometry.location.lng();
          this.zoom = 12;
        }); 
      });
    });
  }
}

ขณะที่ฉัน F12 มัน new google.maps.Geocoder และแสดงให้ฉันเห็นว่ามีอยู่ที่นี่ด้านล่าง
ป้อนคำอธิบายรูปภาพที่นี่ แต่ในความเป็นจริง เมื่อ สร้าง มันจะแสดง ข้อผิดพลาดข้างต้น

อัปเดต 1:

{
  "compileOnSave": false,
  "compilerOptions": {
    "downlevelIteration": true,
    "importHelpers": true,
    "module": "esnext",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "baseUrl": "src",
    "paths": { "@angular/*": ["node_modules/@angular/*"] }
  }
}

นี่คือไฟล์ tsconfig.json

อัปเดต 2:
การใช้ declare var google: any; ใช้งานได้บน
this.geoCoder = new google.maps.Geocoder; และ
const autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement); แต่ล้มเหลวบน google.maps.places.PlaceResult ไม่พบเนมสเปซ ' กูเกิล' const place: google.maps.places.PlaceResult = autocomplete.getPlace();


person Naman Kumar    schedule 26.08.2020    source แหล่งที่มา
comment
คุณสามารถแสดง ts.config ของคุณได้หรือไม่? ปัญหาอาจเป็นได้ว่า ts.config เชิงมุมเริ่มต้นมีประเภท: [] (อาร์เรย์ว่าง) ในกรณีนั้นคุณจะต้องเพิ่ม 'googlemaps' ลงในประเภท   -  person Sherif Elmetainy    schedule 26.08.2020
comment
@SherifElmetainy ฉันคิดว่าไฟล์ tsconfig.json ฉันได้อัปเดตด้านบนสำหรับการอ้างอิง   -  person Naman Kumar    schedule 26.08.2020
comment
ในภาพหน้าจอของคุณ ไม่ใช่คลาส google.maps แต่เป็นเพียงประเภทเท่านั้น และข้อผิดพลาดของคุณแจ้งว่าคุณไม่ได้นำเข้า Google Maps ไปยังส่วนประกอบของคุณ   -  person vitaliy kotov    schedule 26.08.2020
comment
ฉันได้อธิบายไปแล้วหากไม่ได้นำเข้าส่วนประกอบของฉัน แล้วทำไมมันถึงเปลี่ยนเส้นทางไปเมื่อฉันใช้ F12 ไปที่ index.d.ts .. ?   -  person Naman Kumar    schedule 27.08.2020
comment
@vitaliykotov มันควรแสดงข้อผิดพลาดในหน้า typescript ด้วย .. ?   -  person Naman Kumar    schedule 27.08.2020
comment
@นามาน. ถ้าอย่างนั้นปัญหาอาจอยู่ใน tsconfig.app.json?   -  person Sherif Elmetainy    schedule 27.08.2020
comment
@SherifElmetainy ฉันจะหา tsconfig.app.json ของฉันในเชิงมุม 8 ได้ที่ไหนเพื่อที่ฉันจะได้อัปเดตคำถาม ..   -  person Naman Kumar    schedule 27.08.2020
comment
ฉันได้ตรวจสอบวิธีใช้แพ็คเกจ agm และพบ ตัวอย่าง ดูเหมือนว่าปัญหาอยู่ในคอมไพเลอร์ซึ่งไม่พบตัวแปร google แม้ว่าแพ็คเกจจะเพิ่มลงในแอปก็ตาม ดังนั้นคุณต้องประกาศตัวแปรนี้เช่น declare var google: any;   -  person vitaliy kotov    schedule 27.08.2020
comment
@vitaliykotov ฉันลองสิ่งนี้แล้วใช้งานได้บน (ใหม่ google.maps.Geocoder) และ (ใหม่ google.maps.places.Autocomplete(this.searchElementRef.nativeElement)) แต่ใน (google.maps.places.PlaceResult = เติมข้อความอัตโนมัติ getPlace();) มันแสดงข้อผิดพลาดของ map not found   -  person Naman Kumar    schedule 28.08.2020
comment
@vitaliykotov ฉันจะอัปเดตคำถามอีกครั้ง   -  person Naman Kumar    schedule 28.08.2020
comment
ฉันขอแนะนำให้ลองใช้วิธีนี้   -  person vitaliy kotov    schedule 28.08.2020
comment
@vitaliykotov ฉันได้ตอบคำถามของตัวเองแล้วและพบวิธีแก้ไขแล้ว   -  person Naman Kumar    schedule 29.08.2020


คำตอบ (1)


พบวิธีแก้ปัญหาใน tsconfig.app.json.

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "types": ["googlemaps"]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

เพิ่มเฉพาะ "googlemaps" ใน ประเภท ที่หายไปและข้อผิดพลาดหายไป ไม่จำเป็นต้องเพิ่ม declare var google: any; จะใช้งานได้ดีหากไม่มี

person Naman Kumar    schedule 29.08.2020