效果派出了无效的动作:

时间:2019-02-01 14:52:43

标签: javascript angular typescript ngrx ngrx-effects

我使用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 $”调度了无效的操作:...

enter image description here

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

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

1 个答案:

答案 0 :(得分:2)

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

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