0

我正在使用使用ngrx的角度应用程序进行开发。我已经定义了下面的约定来实现加载指示器:

  • 每个实体的初始状态设置为 null
  • 在效果开始时将其设为空对象
  • 在效果完成后用获取的数据填充它

现在我的效果之一是:

  @Effect()
  LoginUser$ = this._actions$.pipe(
    ofType<LoginUser>(EUserActions.LoginUser),
    switchMap((params) => { new LoginUserSuccess(<IUser>{}); return of(params); }), // for loading indicator to be shown
    switchMap((params) => this._userService.loginUser(params.payload)),
    switchMap((currentUser: IUser) => of(new LoginUserSuccess(currentUser)))
  )

但是第一个 switchMap 中的 reducer 调用没有发生。问题是什么。

4

2 回答 2

0

我终于以其他方式解决了我的问题。我现在正在调度另一个动作,在主要动作中更新状态。例如,这就是我的做法:

用户服务.ts

export class UserService {
  constructor(private _store: Store<IAppState>) { }

  loginUser(model): void {
    this._store.dispatch(new AddBusy(EUserActions.LoginUser));
    this._store.dispatch(new LoginUser(model));
  }

  getAllUsers(): void {
    this._store.dispatch(new AddBusy(EUserActions.GetAllUsers));
    this._store.dispatch(new GetAllUsers());
  }
}

用户操作.ts

export class UserEffects {

  @Effect()
  LoginUser$ = this._actions$.pipe(
    ofType<LoginUser>(EUserActions.LoginUser),
    switchMap((params) => this._userLogic.loginUser(params.payload)),
    switchMap((currentUser: IUser) => { this._store.dispatch(new RemoveBusy(EUserActions.LoginUser)); return of(currentUser); }),
    switchMap((currentUser: IUser) => of(new LoginUserSuccess(currentUser)))
  )

  @Effect()
  getAllUsers$ = this._actions$.pipe(
    ofType<GetAllUsers>(EUserActions.GetAllUsers),
    switchMap(() => this._userLogic.getAllUsers()),
    switchMap((users: IUser[]) => { this._store.dispatch(new RemoveBusy(EUserActions.GetAllUsers)); return of(users); }),
    switchMap((users: IUser[]) => of(new GetAllUsersSuccess(users)))
  )

  constructor(
    private _userLogic: UserLogic,
    private _actions$: Actions,
    private _store: Store<IAppState>,
  ) { }
}

这很好地解决了我的问题。

于 2019-05-17T11:37:14.067 回答
0

效果是一个流,只有流中的最后一个动作会被调度。

对于您的情况,您可以LoginUser在减速器中收听并清空您的状态。

于 2019-05-16T06:16:53.547 回答