【发布时间】:2020-06-29 14:25:36
【问题描述】:
我使用一个按钮来initialiseGrid()。在通话进行时不禁用按钮,我想等待第一个响应返回,然后触发第二个请求,或者(即使触发多个请求)按照请求发生的确切顺序处理响应。我实际上尝试使用一些 rxjs 运算符而不是 map,但我很确定我没有正确使用它们。
2020 年 7 月 20 日更新:
我想exhaustMap 是正确的操作符。我应该如何在query 或fetch 方法中使用它?
请在下面找到我的代码:
APs.service.ts
public query(token: string, tableName: string, state: any): void {
console.log("state in query", state);
this.fetch(token, tableName, state)
.subscribe((x: any) => {
console.log("response data", this.data);
super.next(x);
this.isLoading = false;
this.mediatorService.sendMessage("APsRefreshed");
},
(err: any) => {
console.log("err", err);
this.isLoading = false;
}
);
}
public fetch(token: string, tableName: string, state: any): Observable<any> {
let queryStr = `${toODataString(state)}&$count=true`;
queryStr = queryStr.replace(/('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')/ig, function (x) {
return x.substring(1, x.length - 1);
});
queryStr = queryStr.replace(/substringof\((.+),(.*?)\)/, "contains($2,$1)");
const regex = /T00:00:00\.000Z/gi;
const noTimeZoneQueryStr = queryStr.replace(regex, '');
let fetchCallResult = this.http
.get(`${this.BASE_URL}` + `/` + token + `${tableName}&${noTimeZoneQueryStr}`)
.pipe(
map((response: any) => ((<any>this.data) = {
data: response['value'],
total: /*response['value'].length*/parseInt(response['@odata.count'], 10)
}
)),
//tap(() => this.isLoading = false)
);
return fetchCallResult;
}
APs.component.ts
public initialiseGrid() {
this.APsService.isLoading = true;
this.gridViewAPs = this.APsService;
this.APsService.query(this.token, this.tableName, this.state);
}
APs.template.html
<kendo-grid #gridView
[data]="gridViewAPs | async"
[loading]="APsService.isLoading"
...
>
提前谢谢你。
【问题讨论】:
-
我在这里只能看到一个 http 调用,第二个调用应该在哪里?
-
没错。我的意思是同一服务的第二次(第 3 次、第 4 次等)调用。