I want to access to the locationRoot properties.
How can I do it?
[Service Code]
getCityByLocationRootName(locationRoot: LocationRoot) {
const behaviourSubjectCity = new BehaviorSubject<City>(this.city);
const url = this.API_GEOCODE + locationRoot.name + '&appid=' + this.API_KEY;
this.httpClient
.get<City>(url).subscribe({
next: city => behaviourSubjectCity.next(city),
error: error => console.error(error)
});
return behaviourSubjectCity;
}
[Component Code]
getLocationRootByName() {
const locationRoot = this.dataService.getCityByLocationRootName(this.locationRoot);
}
>Solution :
You don’t need to use subject, when you can do
return this.httpClient.get<City>(url)
And after subscribe it into your component.
getLocationRootByName() {
let locationRoot;
this.dataService.getCityByLocationRootName(this.locationRoot).subscribe({
next: city => locationRoot = city,
error: error => console.error(error)
});
}
If you want to handle error in your service, so you can use pipe to do that.