【发布时间】:2018-09-08 05:06:08
【问题描述】:
我正在用 RxJS 做一个分页器,我在任何时候调用一个新页面时都使用一个主题来集中。在每个事件上,我都使用exhaustMap 来检索页面。这样可以防止同一页面多次触发 getPage http 调用。
this._nextPage$.pipe(
exhaustMap(nextPageNumber => this.getPage(nextPageNumber))
).subscribe();
但我还想在每个待处理的 http 上显示一个微调器。
通过这段代码,如何到达合并后的http Observable的订阅? (为了将挂起的组件绑定到订阅)
提前谢谢!
[编辑]
我需要/更喜欢使用本身持有待处理状态的订阅,主要有两个原因:
- 我已经使用了几个基于订阅的自定义组件/指令
- 由于它在许多不同的地方都使用过,我希望在没有太多样板代码的情况下管理挂起状态...
这是一个显示待处理操作的组件的简单示例
@Component({
selector: 'anie-busy',
templateUrl: './busy.component.html',
styleUrls: ['./busy.component.scss']
})
export class BusyComponent implements OnChanges {
@Input() subscription;
isPending = false;
constructor() { }
ngOnChanges() {
if (this.subscription) {
this.isPending = true;
this.subscription.add(() => this.isPending = false);
} else {
this.isPending = false;
}
}
}
【问题讨论】: