【问题标题】:Angular RxJS: Merging the results of 2 observablesAngular RxJS:合并 2 个 observables 的结果
【发布时间】:2020-08-24 22:47:48
【问题描述】:

我有 2 个 Observable,我希望看看两者最终是否都是“真实的”。但是,http get observal 永远不会触发?

    const storeAuth = this.store.isLoggedIn$;
    const heartBeatAuth = this.http.get<boolean>('/api/auth/heartbeat');

    return merge(storeAuth, heartBeatAuth).pipe(
      map((a, b) => {
        if (a && b) {
          return true;
        }
        this.router.navigateByUrl('/login');
        return false;
      }),
      take(1)
    );

【问题讨论】:

    标签: angular rxjs angular2-observables


    【解决方案1】:

    merge 不会像那样发出 a 和 b,如果您希望同时获得两个结果,请使用 combineLatest。合并将从任一流发出第一个结果,然后发出下一个。

    return combineLatest([storeAuth, heartBeatAuth]).pipe(
          map(([a, b]) => {
    

    combineLatest 发出一个数组,因此您可以使用 [a, b] 将数组中的 a 和 b 解构出来

    在订阅返回的 observable 之前,不会触发任何 http 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      相关资源
      最近更新 更多