作者:米米清澈_109 | 来源:互联网 | 2022-12-07 20:20
我在制作HTTP请求的效果中一个接一个地链接动作时遇到了一些麻烦.
这是效果代码:
export class UserEffects {
@Effect()
update$: Observable = 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) {}
}
问题是这两个动作是同时被调用的.LoginStateChange操作取决于要完成的UserChanged操作.
我怎样才能做到这一点?
谢谢!
1> timdeschryve..:
您可以按照Austin的帖子中的说明返回多个操作:从NGRX效果调度多个操作
@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')),
])
);