角路由解析器rxjs

时间:2019-01-03 19:57:20

标签: angular rxjs

我的要求是让用户在单击特定链接时切换帐户。要完全切换帐户,我需要调用两个冷的http observables,然后加载数据。顺序对所有三个请求都很重要。

  1. 第一个switchAccount $
  2. 第二个updateSession $

然后,我只需要使pageData $冷可见即可。我目前正在使用解析器,但是在使用rxjs进行构造时遇到困难。我只关心组件中的最终页面数据,但只需要确保用户切换帐户并且会话已更新,否则后端api调用将无法获取页面数据。

我已尝试执行以下操作,但无法正常工作:

resolve(route): Observable<any> {
    const switchAccount$ = this.userService.switch('account2').pipe(last());
    const updateSession$ = this.userService.updateSesh();
    const pageData$ = this.pageService.getData();
    const switchAndUpdate$ = updateSession$.pipe(skipUntil(switchAccount$));


    return pageData$.pipe(skipUntil(switchAndUpdate$.pipe(last()); // problem is that switch account and page data get cancelled somehow??

}

2 个答案:

答案 0 :(得分:1)

您可以为此使用switchMap:

return obs1$.pipe(
    switchMap(() => obs2$),
    switchMap(() => obs3$),
)

答案 1 :(得分:1)

您可以结合观察到的东西:

resolve(route): Observable<any> {
    const switchAccount$ = this.userService.switch('account2'); // I think last is not helpful
    const updateSession$ = this.userService.updateSesh();
    const pageData$ = this.pageService.getData();

    return switchAccount$.pipe(
         switchMap(() => updateSession$), // Or I think with margeMap works too
         switchMap(() => pageData$),
         catchError(error => of(error))
    )
}
相关问题