【问题标题】:Result from ActiveRoute needed for two independent succeeding queries两个独立的后续查询需要来自 ActiveRoute 的结果
【发布时间】:2017-12-28 00:25:15
【问题描述】:

当我需要在 Observable 上使用 switchMap 两次(独立查询)时,最佳做法是什么。我需要从 activeRoute.ParamMap... 返回的 Id 用于两个独立的查询。只有一个查询,我会执行以下操作:

this.route.paramMap
.switchMap((params: ParamMap) =>
  this.service.getSomeInformation(params.get('id')));

现在我需要从路由返回的 ID 用于另一个查询

this.route.paramMap
.switchMap((params: ParamMap) =>
  this.AnotherService.getSomeMoreInformation(params.get('id')));

所以我需要从 paramMap 中提取的 Id 作为服务和 anotherService 的输入。

最佳做法是什么?把它们锁起来?来自 Service 和 AnotherService 的 Observables 的时间无关紧要。

由于这是我在这里的第一个问题,请让我知道在下一个问题中我可以做得更好。

谢谢, 史蒂芬

【问题讨论】:

  • 将id值存储在一个observable中,然后用那个+switchMap来使用服务?

标签: angular rxjs angular2-observables angular-router switchmap


【解决方案1】:

您可以使用合并运算符来组合其他可观察对象

this.route.paramMap
.switchMap((params: ParamMap) =>
    Rx.Observable.merge(service1(params.get('id'), service2(params.get('id'))
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 2011-08-19
    • 2014-10-29
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多