【问题标题】:Angular Resolver with related Observables using withLatest [duplicate]使用 withLatest 具有相关 Observable 的 Angular Resolver [重复]
【发布时间】:2026-01-24 04:45:01
【问题描述】:

我的解析器的第二个 observable 必须使用第一个 observable 的结果。我不知道如何将该数据传递给第二个 observable:

  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)

        )
    );
  }

感谢您的帮助!

【问题讨论】:

  • 是的。迈克尔的回答也是如此。谢谢。

标签: angular rxjs resolve


【解决方案1】:

如果一个 observable 依赖于另一个 observable 的发射,您需要使用任何 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))
  );
}  

【讨论】:

  • 确实不需要withLatest 谢谢你的链接:)