以角度调度动作的正确方法是什么?

use*_*513 5 effects ngrx ngrx-effects angular ngrx-store

你能告诉我发送动作的正确方法是什么吗?

在组件中,我们喜欢使用storeusing store.dispatchthan action。

onLoginButtonClick(user: Authenticate) {
    this.store.dispatch(new Authctions.Login(user));
  }
Run Code Online (Sandbox Code Playgroud)

现在使用ngEffect我们调度动作而不使用store只使用new Action name为什么?

export class AuthEffect {
  @Effect()
  login$ = this.actions$.pipe(
    ofType(AuthActionsTypes.LOGIN),
    map((action: Login) => action.payload),
    switchMap((auth: Authenticate) => {
      return this.authService.login(auth).pipe(
        map(user => new LoginSuccess({user})),
        catchError(error => of(new LoginFailure(error)))
      );
    })
  );
Run Code Online (Sandbox Code Playgroud)

为什么它是这样使用的

 new LoginSuccess({user})
Run Code Online (Sandbox Code Playgroud)

我们可以这样使用this.store.dispatch( new LoginSuccess({user}))吗?实际上?

任何更新?

Wan*_*rpe 5

效果返回动作,就像这里的文档一样。

Ngrx 将在内部调度从您的效果返回的动作。

您可以直接this.store.dispatch( new LoginSuccess({user}))在效果中使用,但您不应该这样做,因为它不需要,只会使您的代码变得混乱

如果您需要从您的效果中分派多个动作,您可以执行以下操作:

@Effect() save = this.update$.pipe(
   map(action => action.payload),
   switchMap(payload => this.myService.save(payload)),
   switchMap(res => [
       new NotificationAction('save success'),
       new SaveSuccessAction(res)
   ])
);
Run Code Online (Sandbox Code Playgroud)