gpt4 book ai didi

angular - 使用 createReducer 函数时为生产构建 angular+ngrx 8 时出错

转载 作者:太空狗 更新时间:2023-10-29 17:13:11 24 4
gpt4 key购买 nike

目前,我正在尝试使用新的 NgRX creator 函数构建 Angular + NgRX 8 应用程序。但是当我为生产构建它时,出现以下错误:

Function calls are not supported in decorators but 'createReducer' was called in 'reducers'.

在开发模式下完全没有问题。

请求reducer看起来像

export interface State extends EntityState<Request> {
loading: boolean;
error: any;
}
export const initialState = adapter.getInitialState({
loading: false,
error: null
});

export const reducer = createReducer(
initialState,
on(RequestsActions.loadRequestsSuccess, (state, { requests }) => adapter.addAll(requests, {...state, loading: false})),
on(RequestsActions.loadRequestsFailed, (state, { error }) => ({...state, error, loading: false})),
on(RequestsActions.deleteRequestSuccess, (state, { id }) => adapter.removeOne(id, state))
);

并与其他 reducer 组成一个 index.ts 文件

export const reducers = {
requests: reducer
// [...]
}

StoreModule 是这样用 reducers 映射导入的

@NgModule({
imports: [
CommonModule,
StoreModule.forFeature('requests', reducers),
EffectsModule.forFeature(effects),
// [...]
]
})
export class RequestsModule {}

你知道发生了什么事吗?谢谢,干杯!

最佳答案

您需要像这样将 reducer 包装为函数调用:

const yourReducer = createReducer(
initialState,
on(RequestsActions.loadRequestsSuccess, (state, { requests }) => adapter.addAll(requests, {...state, loading: false})),
on(RequestsActions.loadRequestsFailed, (state, { error }) => ({...state, error, loading: false})),
on(RequestsActions.deleteRequestSuccess, (state, { id }) => adapter.removeOne(id, state))
);

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

查看官方文档-

https://ngrx.io/guide/store/reducers#creating-the-reducer-function

关于angular - 使用 createReducer 函数时为生产构建 angular+ngrx 8 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56993101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com