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

Angular RxJS: Merging the results of 2 observables

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

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

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

combineLatest 发出一个数组,因此您可以使用 [a, b]

从数组中解构 a 和 b

在订阅返回的可观察对象之前,不会触发任何 http 请求。