concatAll是否是正确的RxJS运算符以保留顺序?

时间:2017-01-28 11:37:16

标签: angular rxjs rxjs5

我有以下 rxjs 可观察链:

  ngOnInit() {
    this.route.params.map(params => params['userAccountToken'])
      .switchMap(userAccountToken => this.userAccountService.activateAccount(userAccountToken))
      .switchMap(() => this.route.params.map(params => params['userAccountToken']))
      .switchMap(userAccountToken => this.signinService.signinByUserAccountToken(userAccountToken))
      .subscribe(() => this.router.navigate(['/dashboard']));
  }

仅供参考,route的类型为:ActivatedRoute

问题是我正在尝试重用以下observable:

route.params.map(params => params['userAccountToken'])

请注意我是如何复制此observable的:首先是为了激活帐户然后再登录用户......

我试图找到一种方法来重用令牌的值,但我不确定使用哪个运算符。

需要保留调用的顺序。这里concatAll是否合适?

请参阅下面的另一项尝试:

  ngOnInit() {
    this.route.params.map(params => params['userAccountToken'])
      .mergeMap(userAccountToken => Observable.concatAll(
        this.userAccountService.activateAccount(userAccountToken),
        this.sessionService.signinByUserAccountToken(userAccountToken)
      ))
      .subscribe(() => this.router.navigate(['/dashboard']));
  }

此外,我需要mergeMap上的switchMaproute.param.map...吗?

1 个答案:

答案 0 :(得分:2)

你可以,例如嵌套两个switchMaps:

this.route.params
    .take(1)
    .pluck('userAccountToken')
    .switchMap(userAccountToken => {
        return this.userAccountService.activateAccount(userAccountToken)
            .switchMapTo(this.signinService.signinByUserAccountToken(userAccountToken))
    })
    .subscribe(() => this.router.navigate(['/dashboard']));