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

时间:2019-07-11 15:54:36

标签: angular typescript decorator ngrx

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

  

装饰器中不支持函数调用,但在“ 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
  // [...]
}

并使用类似reducers的地图导入StoreModule

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

您知道发生了什么吗?谢谢,加油!

1 个答案:

答案 0 :(得分:3)

您需要像这样将减速器包装为函数调用:

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