使用 createReducer 函数时构建用于生产的 Angular+ngrx 8 时出错

2024-06-01

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

装饰器中不支持函数调用,但在“reducers”中调用了“createReducer”。

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

请求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))
);

并由一个index.ts与其他减速器一起文件

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

StoreModule 是通过减速器映射导入的,如下所示

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

你知道发生了什么事吗?谢谢并欢呼!


您需要将您的减速器包装为函数调用,如下所示:

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 https://ngrx.io/guide/store/reducers#creating-the-reducer-function

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 createReducer 函数时构建用于生产的 Angular+ngrx 8 时出错 的相关文章

随机推荐