Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度9-属性';订阅';不存在于类型';无效';_Angular_Angular9 - Fatal编程技术网

Angular 角度9-属性';订阅';不存在于类型';无效';

Angular 角度9-属性';订阅';不存在于类型';无效';,angular,angular9,Angular,Angular9,我得到以下错误: 类型“void”上不存在属性“subscribe”。ts(2339) 当我试图通过订阅自动链接功能(如下)来使用它时: this.AutoLocate().subscribe(data => { this.GotLoc = data}); 下面是AutoLocate函数,也是一个GetAddress函数,我用它来获得一个使用Lat/Long和Address的完整模型 AutoLocate() { let AutoLocatedLocation: PlaceL

我得到以下错误:

类型“void”上不存在属性“subscribe”。ts(2339)

当我试图通过订阅自动链接功能(如下)来使用它时:

this.AutoLocate().subscribe(data => { this.GotLoc = data});
下面是AutoLocate函数,也是一个GetAddress函数,我用它来获得一个使用Lat/Long和Address的完整模型

  AutoLocate() {
    let AutoLocatedLocation: PlaceLocation;
    if(!Capacitor.isPluginAvailable('Geolocation')) {
      this.Alert.create({header: 'Location Service Error', message: 'Could not initialize Location Services! Please call support!'})
      return;
    }
    Plugins.Geolocation.getCurrentPosition().then(GeoPosition => {
      return this.coordinates = {lat: GeoPosition.coords.latitude, lng: GeoPosition.coords.longitude};
    }).then(coords => {
      this.GetAddress(coords.lat, coords.lng).subscribe(res => {
        this.Address = res;
        return AutoLocatedLocation = { lat: coords.lat, lng: coords.lng, address: res};
      });
    }).catch(error => {
      this.Alert.create({header: 'Location Service Error', message: 'Could not initialize Location Services! Please call support!'})
      console.log("Location: ", error);
    });
  }

  private GetAddress(lat: number, lng: number) {
    return this.http.get<any>(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${environment.googleMapsAPIKey}`)
    .pipe(map(geoData => {
      if (!geoData || !geoData.results || geoData.results.length === 0) {
        return null;
      }
      return geoData.results[0].formatted_address;
    }));
  }
  
AutoLocate(){
让自动定位:PlaceLocation;
如果(!capactor.isPluginAvailable('Geolocation')){
this.Alert.create({标题:“位置服务错误”,消息:“无法初始化位置服务!请呼叫支持!”})
返回;
}
Plugins.Geolocation.getCurrentPosition().then(GeoPosition=>{
返回this.coordinates={lat:geopposition.coords.latitude,lng:geopposition.coords.longitude};
}).然后(coords=>{
this.GetAddress(coords.lat,coords.lng).subscribe(res=>{
this.Address=res;
返回AutoLocatedLocation={lat:coords.lat,lng:coords.lng,地址:res};
});
}).catch(错误=>{
this.Alert.create({标题:“位置服务错误”,消息:“无法初始化位置服务!请呼叫支持!”})
日志(“位置:”,错误);
});
}
专用地址(lat:编号,lng:编号){
返回this.http.get(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${environment.googleMapsAPIKey}`)
.pipe(地图(地理数据=>{
如果(!geoData | | |!geoData.results | | geoData.results.length==0){
返回null;
}
返回geoData.results[0]。格式化的\u地址;
}));
}

我做错了什么?

看来你把承诺和可观察的东西混为一谈了。此外,
AutoLocate()
函数目前没有返回任何内容

您可以将可观察转换为承诺,也可以将可观察转换为承诺。我会使用RxJS
from
函数来实现前者。然后可以应用必要的运算符来转换数据

import{of,from,NEVER}from'rxjs';
从“rxjs/operators”导入{catchError,map,switchMap};

AutoLocate():Observable{//看起来您将承诺与Observable混为一谈。此外,
AutoLocate()
函数目前没有返回任何内容

您可以将observable转换为promise,也可以将observable转换为promise。我将使用RxJS
from
函数执行前者。然后您可以应用必要的运算符来转换数据

import{of,from,NEVER}from'rxjs';
从“rxjs/operators”导入{catchError,map,switchMap};

AutoLocate():Observable{//
this.AutoLocate()
不返回Observable,因此返回错误消息。
this.AutoLocate()
不会返回可观察到的,因此会出现错误消息。我显然需要研究可观察到的和承诺-我仍然是一个新手。你能发布一两个我应该阅读以更好地学习的链接吗?几乎可以!但是,我得到一个错误:(参数)coords:{lat:number;lng:number;}类型的参数'(coords:{lat:number;lng:number;})=>void'不可分配给类型的参数'(值:{lat:number;lng:number;},索引:number)=>observateInput'。类型'void'不可分配给类型'observateInput'。ts(2345)可以找到很多资源。以下是我对RxJS的主观建议,并承诺错误很可能意味着您缺少
map
操作符中对象周围的括号。我编辑了
map
操作符,以更清楚地说明对象是如何返回的。说到这个,您实际上不需要他是这里的
map
操作员。对象转换可以在
switchMap
中完成。我已经编辑了答案,删除了
map
。请看看更改是否有效。我显然需要研究可观察到的内容和承诺-我仍然是一个新手。你能发布一两个链接,我应该阅读以更好地学习吗?几乎可以工作!但是,我得到一个错误:(参数)coords:{lat:number;lng:number;}类型的参数'(coords:{lat:number;lng:number;})=>void'不可分配给类型的参数'(值:{lat:number;lng:number;},索引:number)=>ObservableInput'。类型'void'不可分配给类型'ObservableInput'。ts(2345)可以找到很多资源。以下是我对RxJS的主观建议,并承诺错误很可能意味着您缺少
map
操作符中对象周围的括号。我编辑了
map
操作符,以更清楚地说明对象是如何返回的。说到这个,您实际上不需要他是这里的
map
操作符。对象转换可以在
switchMap
中完成。我已经编辑了答案以删除
map
。请查看更改是否有效。