我的效果似乎抛出了一个操作必须具有类型属性
,这似乎是我的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 })),
))
);
关于javascript - 操作必须具有类型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125205/