gpt4 book ai didi

angular - 找不到名称 'google' Angular 8

转载 作者:行者123 更新时间:2023-12-04 11:49:48 31 4
gpt4 key购买 nike

因为它在构建过程中显示错误。

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

27 this.geoCoder = new google.maps.Geocoder;~~~~~~app/pages/TestPage4/TestPage4.ts:29:32 - error TS2304: Cannot find name 'google'.

29 const autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement);~~~~~~app/pages/TestPage4/TestPage4.ts:33:24 - error TS2503: Cannot find namespace 'google'.

33 const place: 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并向我展示它存在于下面。
enter image description here
但实际上当 已建 它显示了上述错误。
更新 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 上失败找不到命名空间“google” const place: google.maps.places.PlaceResult = autocomplete.getPlace();

最佳答案

中找到了解决方案tsconfig.app.json .

{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"types": ["googlemaps"]
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
仅添加 "googlemaps"类型 丢失了,错误消失了。无需添加 declare var google: any;没有它会很好用。

关于angular - 找不到名称 'google' Angular 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63604420/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com