我已经检查了SO的所有可能答案,但似乎无法正确解决此问题。
我正在将有效负载传递给操作'GetUser'。
我希望此有效负载通过效果传递,并最终通过http请求发送到API,但是似乎根本没有触及我的效果。控制台或ngrx开发工具中什么都没有。
我的代码看起来不错,我没有编译错误-怎么了?
这里是效果:
@Injectable()
export class LoginEffects {
env = environment;
constructor(private actions: Actions, private http: HttpClient, private store: Store<any>) {
}
@Effect()
getUser$: Observable<Action> = this.actions.pipe(
ofType<GetUser>(LoginActions.GET_USER),
switchMap((action) => {
console.warn(action.payload);
console.warn(action.payload);
console.warn(action.payload);
return this.http.post(this.env.urlDefault + '/authenticate', action.payload).pipe(
map((data) => new LoginActions.GetUserSuccess( data )),
catchError(error => of(new LoginActions.GetUserFail( error ))));
}));
}
这里是动作:
import { Action } from '@ngrx/store';
export const GET_USER = 'GET_USER';
export const GET_USER_FAIL = 'GET_USER_FAIL';
export const GET_USER_SUCCESS = 'GET_USER_SUCCESS';
export class GetUser implements Action {
readonly type = GET_USER;
constructor(public payload: any) {
console.log('GetUser Action Hit!');
}
}
export class GetUserFail implements Action {
readonly type = GET_USER_FAIL;
constructor(public payload: any) {
console.log('GetUserFail Action Hit!');
}
}
export class GetUserSuccess implements Action {
readonly type = GET_USER_SUCCESS;
constructor(public payload: object) {
console.log('GetUserSuccess Action Hit!');
}
}
export type LoginActions = GetUser | GetUserFail | GetUserSuccess;
您是否已在模块中注册效果?将以下行添加到模块导入部分
@NgModule({
imports: [
...
EffectsModule.forRoot([AppEffects])
],
...
})