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

问题描述

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

解决方法

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

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

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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...