如何使用带有ngrx 8.0.0 beta的StoreModule.forFeature注册单个reducer

时间:2019-06-04 20:32:16

标签: angular ngrx ngrx-store ngrx-store-4.0

在我的功能模块中,即时消息使用createReducer方法创建我的减速器,如下所示:

export const reducer = createReducer(
new InitState(),
on(loadData, state => {
return {
...state,
isLoading: true
};
})
);

但是当我在模块类中注册上述减速器时,如下所示:

StoreModule.forFeature('my-module', reducer),

在编译项目时出现以下错误:

ERROR myModule.module.ts(38,47):模板编译“ MyModuleModule”时出错 装饰器不支持函数调用,但在“ reducer”中调用了“ createReducer” 'reducer'在myreducer.reducer.ts(14,24)处调用'createReducer'。

有人知道可能出什么问题吗?或我该如何注册单个减速器,因为我所看到的所有示例都使用带有多个减速器的CombineReducers,我想知道如果仅使用一个减速器,我们该怎么做?

非常感谢!

2 个答案:

答案 0 :(得分:1)

您需要包装减速器,使其与AOT编译兼容:

export function myAOTSafeReducer(
    state: State | undefined,
    action: Action
) {
    return reducer(state, action);
}

然后在状态下引用此导出的命名函数。

答案 1 :(得分:0)

我相信您必须在这里使用函数而不是const。我有这样的错误。因此,您需要将代码更改为类似的内容

export function bookmarkReducer(state = initialState, action: bookMark.Actions) {
    switch (action.type) {
        case bookMark.GET_BOOK_MARK:
            return {
                ...state,
                id: action.payload
            };
        case bookMark.GET_BOOK_MARK_SUCCESS:
            return {
                ...state,
                courseByBookmarkId: action.payload
            };
        case bookMark.GET_BOOK_MARK_BY_USERID_SUCCESS:
            return {
                ...state,
                bookmark: action.payload
            };
        case bookMark.BOOK_MARK_COURSE:
        case bookMark.UNDO_BOOK_MARK_COURSE:
            return {
                ...state,
                userId: action.payload.userId,
                courseId: action.payload.courseId
            };
        default:
            return state;
    }
}