【发布时间】:2019-01-08 06:05:15
【问题描述】:
我的应用中有以下两个 HTTP 服务 RxnsSearchService 和 RxnsSearchHitCountService。
使用forkJoin处理两个请求,如下代码所示。
constructor(
private rxnsSearchService: RxnsSearchService,
private rxnsSearchHitCountService: RxnsSearchHitCountService
) { }
const rxnsObservable: Observable<Array<any>> = this.rxnsSearchService.getReactions(this.searchParams, filters);
const headCountObservable: Observable<number> = this.rxnsSearchHitCountService.getHitCount(this.searchParams, filters);
forkJoin([rxnsObservable, headCountObservable]).pipe().subscribe((results) => { //handling results
},
error => {
console.log(error);
});
每当有新请求到来时,我想取消正在进行的旧请求。谁能帮我解决一下?
export class RxnsSearchService {
sub: Subject<any> = new Subject();
constructor(private httpClient: HttpClient) {}
getReactions(params: Params, offset: number, perPage: number, filters: any) {
const body = {
filters: filters,
query: params.query
};
return this.httpClient.post(environment.rxnsSearch, body).pipe(
map((response: Array<any>) => {
return response;
}),
catchError(error => {
console.log(error);
return throwError(error);
})
);
}
}
export class RxnsSearchHitCountService {
constructor(private httpClient: HttpClient) {}
getHitCount(params: Params, filters: any) {
const body = {
filters: filters,
query: params.query,
};
return this.httpClient.post(environment.rxnsSearchHitCount, body).pipe(
map((response: number) => {
return response;
}),
catchError(error => {
console.log(error);
return throwError(error);
})
);
}
}
【问题讨论】:
-
真正调用 forkJoin 的是什么?从您的代码 sn-p 看来,forkJoin 只被调用过一次。您能否提供其他代码来显示触发 forkJoin 的原因?谢谢!
-
this.route.queryParams.subscribe((params: Params) => { if (Object.keys(params).length > 0) { this.displayreactions(); } }); } ngOnChanges(changes: SimpleChanges) { if (!changes['filters'].isFirstChange()) { this.displayreactions(); } } -
forkJoin从多个方法调用。 -
GitHub link LineNo:123
标签: javascript angular typescript rxjs