使用withLatest与相关Observables的角解析器

问题描述

我的解析器的第二个可观察值必须使用第一个可观察到的结果。我无法弄清楚如何将该数据传递给第二个可观察的对象:

  resolve(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): Observable<[Project[],Owner]> {
    const currentEmail = this.CurrentUserEmail();
    return this.searchService.genericSearchApi('/api/searchproject',...[//I need to pass the user here.]]).pipe(
        withLatestFrom(
            this.userService.getUserByEmail(currentEmail)

        )
    );
  }
@H_502_5@

感谢您的帮助!

解决方法

在一个可观察物取决于另一个可观察物的发射的情况下,您需要使用RxJS高阶mapping operators中的任何一个。

使用switchMap运算符的插图

resolve(
  route: ActivatedRouteSnapshot,state: RouterStateSnapshot
): Observable<[Project[],Owner]> {
  const currentEmail = this.CurrentUserEmail();
  return this.userService.getUserByEmail(currentEmail).pipe(
    switchMap(user => this.searchService.genericSearchApi('/api/searchproject',user))
  );
}