@ngrx中的效果中的连锁动作 [英] Chain Actions in an Effect in @ngrx

查看:56
本文介绍了@ngrx中的效果中的连锁动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行HTTP请求的效果中,我在一个接一个的链接动作上遇到了一些麻烦.

I am having some trouble chaining actions one after the other in an Effect that makes an HTTP-request.

以下是效果代码:

export class UserEffects {

    @Effect()
    update$: Observable<Action> = this.actions$.ofType(user.USERCHANGE).pipe(
        switchMap(data => this.authService.login(data['payload'])),
        map(userInfo => new UserChangedAction(userInfo)),
        tap(() => this.store.dispatch(
             new LoginStateChangeAction(localStorage.getItem('token')))
        )
    );

    constructor(private authService: AuthService, private actions$: Actions,
        public store: Store<fromRoot.State>) {}
}

问题在于两个动作同时被调用. LoginStateChange操作取决于要完成的UserChanged操作.

The problem is both the actions are being called simultaneously. The LoginStateChange action depends on UserChanged action to complete.

我该如何实现?

谢谢!

推荐答案

您可以按照Austin帖子中的说明返回多个操作:

You can return multiple actions as explained in Austin's post: Dispatching Multiple Actions from NGRX Effects

@Effect()
update$ = this.actions$.ofType(user.USERCHANGE).pipe(
  switchMap(data => this.authService.login(data['payload'])),
  switchMap(userInfo => [
    new UserChangedAction(userInfo),
    new LoginStateChangeAction(localStorage.getItem('token')),
  ])
);

这篇关于@ngrx中的效果中的连锁动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆