Javascript 角度-如何使用不同的参数订阅相同的服务响应(多个API调用)?
下面是我的代码片段 角度服务Javascript 角度-如何使用不同的参数订阅相同的服务响应(多个API调用)?,javascript,angular,typescript,rest,web-services,Javascript,Angular,Typescript,Rest,Web Services,下面是我的代码片段 角度服务 export class CarService { constructor(private httpClient: HttpClient) {} // ------------------------------------------------- // define observable Subject(s) which will be subscribed by callers private observableList = new Sub
export class CarService {
constructor(private httpClient: HttpClient) {}
// -------------------------------------------------
// define observable Subject(s) which will be subscribed by callers
private observableList = new Subject<CarModel>();
public getObservableList() {
return this.observableList.asObservable();
}
// -------------------------------------------------
// fetch required required data, then add into observable
public fetchCarInfo(carId: string) {
// call service
const url = '.../api/car/' + carId;
this.httpClient
.get<CarModel[]>(url)
.subscribe(
data => {
this.observableList.next(data);
}
);
}
}
正常溶液要添加到服务中2个可观察对象,请在组件中订阅这两个
但我认为这不是一个干净的解决方案,在服务中添加多个可观测项,相当于使用不同参数的服务调用的数量
有什么建议吗?您为什么要在服务中订阅并使用主题?只需从
fetchCarInfo
返回observable,并让调用者[即组件或其他服务]订阅即可。这样做的具体原因是什么?要处理错误,可以在可观察管道中使用catchError
操作符。您应该只从调用者处订阅。管道中的catchError
操作员也可以在一个位置捕获错误。我可以看出这个主题的用法是不必要的。可观察性最好的东西是通过使用各种操作符合成的能力。删除主题
并返回observable可以使您的问题很容易得到回答。问题是,您正在尝试处理两个可观察对象[subject+api],这可以使用一个可观察对象轻松完成。您可以使用map
操作符准备数据,您可以使用flatMap
操作符组合多个调用,不需要在里面订阅service@ahmednabil88您应该了解如何使用各种操作符来合成最终的可观察对象。您仍然可以处理您的数据,甚至可以调用observable operators链中的一些其他API,而不必进行订阅。只能由调用方完成订阅。订阅服务只是一个糟糕的设计。绝对不需要。浏览ngconf 2019的精彩视频-现在,您决定使用这种方式,最好在CarServcie
中保留数据,而不是调用getobserveList()
方法的observeable
,它执行fetchCarInfo
方法,无需订阅subscribe
和多个API调用。您为什么在服务中订阅并使用subject?只需从fetchCarInfo
返回observable,并让调用者[即组件或其他服务]订阅即可。这样做的具体原因是什么?要处理错误,可以在可观察管道中使用catchError
操作符。您应该只从调用者处订阅。管道中的catchError
操作员也可以在一个位置捕获错误。我可以看出这个主题的用法是不必要的。可观察性最好的东西是通过使用各种操作符合成的能力。删除主题
并返回observable可以使您的问题很容易得到回答。问题是,您正在尝试处理两个可观察对象[subject+api],这可以使用一个可观察对象轻松完成。您可以使用map
操作符准备数据,您可以使用flatMap
操作符组合多个调用,不需要在里面订阅service@ahmednabil88您应该了解如何使用各种操作符来合成最终的可观察对象。您仍然可以处理您的数据,甚至可以调用observable operators链中的一些其他API,而不必进行订阅。只能由调用方完成订阅。订阅服务只是一个糟糕的设计。绝对不需要。浏览ngconf 2019的精彩视频-现在,您决定使用这种方式,最好在CarServcie
中保留数据,而不是调用getobserveList()
方法的observeable
,它执行fetchCarInfo
方法,无需订阅subscribe
和多个API调用。
export class ListPage implements OnInit, OnDestroy {
car1: CarModel[];
car2: CarModel[];
constructor(private carService: CarService) {}
ngOnInit() {
// 1. subscribe Observable Subject
const carsSubscription = this.carService
.getObservableList()
.subscribe(serviceResult => {
// Here we can use returned serviceResult
// but our service will be called 2 times with 2 different parameters!
});
}
// in my business, I need to call the same service 2 times with different parameters
// The issue here, BOTH calls will refresh the same Observable
// so i can not know the returned serviceResult is related to the first or second call?
public coreBusiness(carId1: string, carId2 string) {
// 2. call service method by parameters to fetch/refresh Observable
this.carService.fetchCarInfo(carId1);
this.carService.fetchCarInfo(carId2);
}
}