问题
I use Angular 7.1.4
My Effect:
@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)))
);
})
);
}
And i get this error: ERROR Error: Effect "LoginEffects.loginRequest$" dispatched an invalid action: ...
If i add {dispatch: false} in the @Effect decorator, the error is gone.
Also i get typescript errors if try to access payload properties.
回答1:
All actions must have a type
property to identify them. Looking at the error log your action only contains a payload. Make sure that LoginSuccessAction
and LogOutAction
both have a readonly type
property. Example:
class LoginSuccessAction extends Action {
readonly type = "LOGIN_SUCCESS";
}
来源:https://stackoverflow.com/questions/54481888/effect-dispatched-an-invalid-action