rxjs与角2 - 如何链可观察流
我正在做一个应用程序与angular2和@ ngrx /店...我新rxjs和思考在一种被动的方式...真正挣扎如何observables是链。rxjs与角2 - 如何链可观察流
我有一个权威性的效果......目前看起来像
@Effect() authenticate$ = this.updates$
.whenAction(AuthActions.AUTHENTICATE_REQUEST)
.switchMap(update => this.api.post('/authenticate', update.action.payload)
.map((res:any) => this.authActions.authenticateSuccess(res.json()))
.catch((err:any) => Observable.of(this.authActions.authenticateError(err)))
);
我想要做的是链上的两个额外的动作,当请求成功。
- 成功后用
this.router.navigate(['/'])
导航到“home”页面。 - 存放在localStorage的服务的凭证
this.authService.store(credentials)
什么我需要做的流将这些行动?
假设最后2条语句的执行顺序是无关紧要的,所有你需要做的就是在当前语句的结束追加
.flatMap(() =>
{
this.router.navigate(['/']);
this.authService.store(credentials);
});
谢谢...好吧,会给你一个去。是.switchMap之后?或在捕获后? – markstewie
是的,在.switchMap – hholtij
得到TypeScript错误之后...''()=> void'的参数不能分配给类型为'(value:Action,index:number)=>的参数参数 |承诺 | ArrayLike '.' – markstewie
最好的解决办法是让你的effect
这样并添加另一个反应到这样的成功行动:
@Effect({dispatch: false}) authenticateSuccess$ = this.updates$
.whenAction(AuthActions.AUTHENTICATE_SUCCESS)
.do(credentials => {
this.router.navigate(['/']);
this.authService.store(credentials);
});
凡{dispatch: false}
的意思是“应对这种行动,但不发送另一个”。
您正在调度authenticateSuccess操作,所以您应该在另一个效果中处理此操作。当你有多种认证方式时,这特别有用。 – j2L4e