我有以下 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
上的switchMap
或route.param.map...
吗?
答案 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']));