Ngrx效果不返回数据



我已经开始学习ngrx。我正在创建示例项目,但我遇到了问题,我无法从api调用中获取数据。

loadProducts$ = createEffect(() => { 
console.log("here working")
return this.actions$.pipe(
ofType(ActionTypes.LoadItems),
mergeMap(() =>
this.productsService.getAll().pipe(
map(products => {
console.log(products)
return { type: ActionTypes.LoadSuccess, payload: products };
}),
catchError((eror) => of(eror))
)
)
)
});
getAll() {
return this.http.get('https://xxxxxxx');
}

import { Action } from '@ngrx/store';

interface Product {
id : number;
uid : string;
blend_name: string;
origin: string;
notes: string;
intensifier: string;
}
export enum ActionTypes {
LoadItems = '[Products] Load items from server',
LoadSuccess = '[Products] Load success'
}
export class LoadItems implements Action {
readonly type = ActionTypes.LoadSuccess;
constructor(public payload: Product[]) {}
}
export class GetItems implements Action {
readonly type = ActionTypes.LoadItems;
}
export type ActionsUnion = GetItems | LoadItems; 

加载产品执行并打印控制台日志(这里工作)

虽然效果将返回一个已定义的Action,所以替换这一行:

return { type: ActionTypes.LoadSuccess, payload: products };

return ActionTypes.LoadSuccess({payload: products});

如果它仍然不能工作,请提供操作的定义。

最新更新