Angular RxJS:合并2个可观察对象的结果

时间:2020-08-24 22:47:48

标签: angular rxjs angular2-observables

我有2个可观察的对象,我想看看两者最终是否都为“ true”。 http get observative永远不会触发?

    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)
    );

1 个答案:

答案 0 :(得分:3)

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

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

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

在某些内容订阅了返回的observ之前,不会触发任何http请求。

相关问题