【问题标题】:Is there a way to reach the subscription of a merged Observable (mergeMap/concatMap/exhaustMap)有没有办法订阅合并的 Observable (mergeMap/concatMap/exhaustMap)
【发布时间】: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;
    }
  }

}

【问题讨论】:

    标签: rxjs rxjs5


    【解决方案1】:

    您可以在两者之间使用管道 do 运算符来完成此操作。

    this._nextPage$.pipe(
    tap(()=>{//loading},
    exhaustMap(nextPageNumber => this.getPage(nextPageNumber),
     tap(()=>{// stop loading})
    ).subscribe();
    

    【讨论】:

    • 我想避免这种情况,主要是因为我已经有依赖Subscription 的系统,但我看不到其他任何东西。
    • 仅供参考,使用管道运算符,您应该使用 tap 而不是 do。
    • @Alexandre SIRKO 不太明白你的意思依赖订阅
    • 我使用 Angular 并且我有几个组件来显示挂起状态,它们都接受 Subscription 作为输入。然后我使用 add(aTearDown) 根据 Observable 的分辨率做出反应
    • 查看更多代码,了解是什么让您的案例与众不同。我们经常只是为了各种目的对 observable 进行切片和组合。
    猜你喜欢
    • 2022-01-21
    • 2021-12-29
    • 2017-03-24
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    相关资源
    最近更新 更多