Angular 使用 withLatest 的相关 Observable 解析器
Angular Resolver with related Observables using withLatest
我的解析器的第二个可观察对象必须使用第一个可观察对象的结果。我不知道如何将该数据传递给第二个可观察对象:
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)
)
);
}
感谢您的帮助!
如果一个 observable 依赖于另一个 observable 的发射,您需要使用任何 RxJS 高阶 。
使用 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))
);
}
我的解析器的第二个可观察对象必须使用第一个可观察对象的结果。我不知道如何将该数据传递给第二个可观察对象:
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)
)
);
}
感谢您的帮助!
如果一个 observable 依赖于另一个 observable 的发射,您需要使用任何 RxJS 高阶
使用 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))
);
}