在reducer中注册ngrx操作类



我想将我的ngrx操作从createAction重构为基于类的操作,但我在关联reducer中声明该操作时出错:

export enum ActionTypes {
LOAD_PRODUCTS_FROM_API = '[Products] Load Products From Api'}
export class LoadProductsFromApi implements Action {
readonly type = ActionTypes.LOAD_PRODUCTS_FROM_API;}

const rootReducer = createReducer(
initialState,
on( //ERROR LoadProductsFromApi ERROR //, (state, action) => (<State>{ products: state.products, filterValue: state.filterValue, isLoading: true })));

错误:没有与此调用匹配的重载。重载1(共11个(,'(creator1:ActionCreator<string,FunctionWithParametersType<any[],object>>,reducer:OnReducer<State,[ActionCreator<string,FunctionWithParametersType<any[],object>>]>(:开<gt;',给出以下错误。类型为"typeof LoadProductsFromApi"的参数不可分配给类型为"ActionCreator<字符串,函数WithParametersType<any[],object>gt;'。类型"typeof LoadProductsFromApi"不可分配给类型"FunctionWithParametersType<any[],对象>'。类型"typeof LoadProductsFromApi"没有为签名"(…args:any[](:object"提供匹配项。重载2(共11个(,'(创建者:ActionCreator<string,FunctionWithParametersType<any[],object>>,…rest:(ActionCreator<string,FunctionWithParametersType<any[],object>>|OnReducer<…>([](:打开<gt;',给出以下错误。类型为"typeof LoadProductsFromApi"的参数不可分配给类型为"ActionCreator<字符串,函数WithParametersType<any[],object>gt;'。类型"typeof LoadProductsFromApi"不可分配给类型"FunctionWithParametersType<any[],对象>'。

我的行动.ts

换句话说,我想知道如何在reducer中注册不基于switch语句的操作类。

您可以使用

import { createAction, props } from '@ngrx/store';

export const loadProducts = createAction(
ProductsTypes.request,
props<IProductsRequest>()
);
export const loadProductsSuccess = createAction(
ProductsTypes.requestSuccess,
props<{ products: IProduct[] }>()
);
export const loadProductsFailure = createAction(
ProductsTypes.requestFailure,
props<{ error: any }>()
);

其中

export enum ProductsTypes {
request = '[Products] Load',
requestSuccess = '[Products] Load Success',
requestFailure = '[Products] Load Failure',
}

export interface IProductsRequest {
// your interface
}

然后,您可以使用import { createReducer, on } from '@ngrx/store';作为减速器。

阅读更多关于createAction和createReducer 的信息

最新更新