将第一个可观察结果传递给switchMap中的可观察结果

时间:2020-03-10 13:39:26

标签: javascript rxjs

我正在使用switchMap链接两个可观察对象,但是我需要第一个可观察对象的结果在第二个可观察对象内部可用,以便我可以使用结果映射第二个可观察对象的结果。

在订阅中使用res1启用的语法是什么?

this._signalRService.obs1.pipe(
          takeUntil(this.destroyObservables),
          switchMap(res1 => {
                  if (this.idParam === res1) {
                      return this.getAllCustomCategoryGroups$();
                  }
                  return;
              }
          )).subscribe(groups => {
            let x = groups.map(group => groupId === res1); //res1 needed here to map groups 
      });

三个可观察物

 this._authService.loggedInUser$.pipe(switchMap((loggedInUser: LoggedInUser) => {
      return this._userSerialService.getUserSerial().pipe(switchMap(serial => {
          return this._usersService.getCurrentUser().pipe(switchMap(currentUser => [loggedInUser, currentUser, serial]))
        }),
      );
    })).subscribe(res => {
      console.log(res);
    })

1 个答案:

答案 0 :(得分:1)

您可以将第二个可观察到的发射映射到数组或对象中。结果选择器对您没有帮助:

switchMap(res1 => {
  if (this.idParam === res1) {
    return this.getAllCustomCategoryGroups$().pipe(
      map(res2 => [res1, res2]),
    );
  }
  return EMPTY; // You have to return something here
}).subscribe(([res1, res2]) => { ... })