我已经检查了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;
答案 0 :(得分:1)
您是否已在模块中注册效果? 将以下行添加到模块导入部分
@NgModule({
imports: [
...
EffectsModule.forRoot([AppEffects])
],
...
})