在Angular 7中,处理需要按特定顺序进行的端点调用的最优化方法是什么?
我目前有嵌套的电话,以强制“等待”。但这似乎有点难闻,而且不仅仅只是连续几个电话就不理想。我在其他职位上也浏览过SO,但是大多数都是针对较旧版本的Angular。新版本是否改变了这一点?我也进入了Angular文档,但它们似乎专注于Observables,它对于异步需求似乎很理想。
Get-weather.component.ts
getUserWeather(zipcode) {
this.coordinateService.getCoordinates(zipcode)
.subscribe((data: Weather) => {
this.latitude = data['lat'],
this.longitude = data['lng']
this.weatherService.getWeather(this.latitude, this.longitude)
.subscribe((data: Weather) => {
this.temperature = data['currently'].temperature;
})
})
}
Get-coordinates.service.ts
export class GetCoordinatesService {
apiURL: string = 'https://www.zipcodeapi.com/rest';
constructor(private httpClient: HttpClient) { }
getCoordinates(zipcode: string): Observable<any> {
return this.httpClient.get(`${this.apiURL}/${myAPIkey}/info.json/${zipcode}/degrees`)
}
}
获取用户weather.service.ts
export class GetUserWeatherService {
constructor(private httpClient: HttpClient) { }
apiURL: string = "https://api.darksky.net/forecast";
getWeather(latitude: number, longitude: number): Observable<any> {
return this.httpClient.get(`${this.apiURL}/${myAPIkey}/${latitude},${longitude}`)
}
该代码当前有效,只是感觉像这样的嵌套调用不是最好的方法。我应该如何处理?