【问题标题】:Separate on() function outside of createReducer() function in ngrx在ngrx中的createReducer()函数之外分离on()函数
【发布时间】:2020-02-02 23:24:33
【问题描述】:

是否可以将 on() 参数从 createReducer() 函数中分离出来。

例如,而不是这个;

const yourInformationReducer = createReducer<IYourInformationState>(
    initYourInformationState,
    on(YourInformationStoreAction, (state, { payload }) => ({
        ...state,
        ...payload
    }))
);

为 on() 函数定义一个常量;

const onYourInformationStoreAction = on<IYourInformationState>(
    YourInformationStoreAction,
    (
        state: IYourInformationState,
        { payload }: { payload: IYourInformationFormModel }
    ) => ({
        ...state,
        ...payload
    })
);

然后在createReducer中引用它;

const yourInformationReducer = createReducer<IYourInformationState>(
    initYourInformationState,
    onYourInformationStoreAction 
);

我遇到的问题是,当我在分离函数中设置有效负载对象的类型时,它给出了一个我不知道如何处理的打字稿错误......

错误:(16, 2) TS2345: 类型参数 '(state: IYourInformationState, {有效载荷}:{有效载荷:IYourInformationFormModel; }) => { 名称: 细绳;电子邮件:字符串;联系人号码:字符串;有效性状态: 有效性状态; }' 不可分配给类型参数 '动作创造者> | OnReducer>]>'。输入'(状态: IYourInformationState,{有效载荷}:{有效载荷: IYourInformationFormModel; }) => { 名称:字符串;电子邮件:字符串; 联系人号码:字符串;有效性状态:有效性状态; }' 不是 可分配给类型“ActionCreator>”。 类型 '(state: IYourInformationState, { payload }: { payload: IYourInformationFormModel; }) => { name: 细绳;电子邮件:字符串;联系人号码:字符串;有效性状态: 有效性状态; }' 但在类型 'TypedAction' 中是必需的。

【问题讨论】:

    标签: ngrx


    【解决方案1】:

    想通了……

    function onYourInformationInitAction() {
        return on(YourInformationInitAction, (state: IYourInformationState) => ({
            ...state
        }));
    }
    
    function onYourInformationStoreAction() {
        return on(
            YourInformationStoreAction,
            (state: IYourInformationState, { payload }) => ({
                ...state,
                ...payload
            })
        );
    }
    
    function onYourInformationValidityStatus() {
        return on(
            YourInformationUpdateValidityStatusAction,
            (state: IYourInformationState, { validityStatus }) => ({
                ...state,
                validityStatus
            })
        );
    }
    
    const yourInformationReducer = createReducer<IYourInformationState>(
        initYourInformationState,
        onYourInformationInitAction(),
        onYourInformationStoreAction(),
        onYourInformationValidityStatus()
    );
    

    【讨论】:

      猜你喜欢
      • 2021-05-24
      • 2019-11-21
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      相关资源
      最近更新 更多