【问题标题】:Effect dispatched an invalid action:Effect 调度了一个无效的动作:
【发布时间】:2019-02-01 14:52:42
【问题描述】:

我使用 Angular 7.1.4

我的效果:

@Injectable()
export class LoginEffects {
  constructor(private actions$: Actions, private authService: AuthenticationService) {
  }

  @Effect({dispatch: true})
  loginRequest$: Observable<any> = this.actions$.pipe(
    ofType(LoginActionsType.LOGIN_REQUEST),
    exhaustMap(payload => { // it might be switchMap, the error is the same.
      console.log(payload);
      return this.authService.login(payload.user, payload.password) // TypeScript Errors: Property 'user' does not exist on type 'never'. ; TS2339: Property 'password' does not exist on type 'never'.
        .pipe(
          map(user => new LoginSuccessAction(user)),
          catchError( err => of(new LogOutAction(err)))
        );
    })
  );
}

我得到这个错误:错误错误:效果“LoginEffects.loginRequest$”调度了一个无效的动作:...

如果我在 @Effect 装饰器中添加 {dispatch: false},错误就消失了。

如果尝试访问有效负载属性,我也会收到打字稿错误。

【问题讨论】:

    标签: javascript angular typescript ngrx ngrx-effects


    【解决方案1】:

    所有操作都必须有一个type 属性来识别它们。查看错误日志,您的操作仅包含有效负载。确保LoginSuccessActionLogOutAction 都具有只读的type 属性。示例:

    class LoginSuccessAction extends Action {
      readonly type = "LOGIN_SUCCESS";
    }
    

    【讨论】:

    • 你是对的。 LoginSuccessAction 中有错字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2019-12-26
    • 2018-11-10
    • 2020-10-15
    • 1970-01-01
    • 2020-10-23
    相关资源
    最近更新 更多