我的效果似乎是引发Actions must have a type property
,这似乎是导致问题的原因,但我的RegisterSuccess
动作似乎是我所说的方式,因此我尝试了一些解决方案,但在我看来,它们似乎不起作用。
影响:
@Effect()
register = this.actions$.pipe(
ofType(AuthActionTypes.REGISTER),
switchMap(action => this.auth.register(action.registration).pipe(
map(result => ([
{ type: AuthActionTypes.REGISTER_SUCCESS, user: result }
])),
catchError(result => ([
{ type: AuthActionTypes.REGISTER_FAIL }
])),
))
);
行动:
export class Register implements Action {
readonly type = AuthActionTypes.REGISTER;
constructor(public registration: Registration) {}
}
export class RegisterSuccess implements Action {
readonly type = AuthActionTypes.REGISTER_SUCCESS;
constructor(public user: User) {}
}
export class RegisterFail implements Action {
readonly type = AuthActionTypes.REGISTER_FAIL;
constructor() {}
}
服务:
register(user: Registration): Observable<any> {
return this.api.post('auth/register', user).pipe(map(res => res.data));
}
最佳答案
您的效果是针对NgRx版本8.3.0编写的
您的操作是针对NgRx版本7.4.0编写的
“ switchMap(action => this.auth.register(action.registration).pipe(”
你这里不需要管道
对于NgRx版本8.3.0,应该是这样的:
行动:
import { createAction, props } from '@ngrx/store';
export const register = createAction(AuthActionTypes.REGISTER());
export const registerSuccess = createAction(AuthActionTypes.REGISTER_SUCCESS, props<{user: User}>());
export const registerFail = createAction(AuthActionTypes.REGISTER_FAIL);
效果:
@Effect()
register = this.actions$.pipe(
ofType(register.type),
mergeMap(action => this.auth.register(action.registration),
map(result => ({ type: registerSuccess.type, user: result })),
catchError(result => ({ type: registerFail.type })),
))
);