简体   繁体   English

Angular RxJS:合并 2 个 observables 的结果

[英]Angular RxJS: Merging the results of 2 observables

I have 2 Observable and I wish to see if both end up being "true".我有 2 个 Observable,我想看看它们是否最终都是“真实的”。 The http get observal never fires though?虽然 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 does not emit an a and b like that, use combineLatest if you want both results at the same time. merge 不会发出 a 和 b 那样的,如果您想要同时获得两个结果,请使用 combineLatest 。 Merge will emit the first result from either stream then the next.合并将从任一流发出第一个结果,然后是下一个。

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

combineLatest emits an array so you can destructure a and b out of the array with [a, b] combineLatest 发出一个数组,因此您可以使用[a, b]从数组中解构 a 和 b

No http requests will be fired until something subscribes to the returned observable.在订阅返回的 observable 之前,不会触发任何 http 请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM