Error building angular+ngrx 8 for production when using createReducer function

点点圈 提交于 2020-01-14 07:03:10

问题


Currently I am trying to build an Angular + NgRX 8 application with the new NgRX creator functions. But when I am building this for production, there appears the following error:

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

In development mode there is no problem at all.

The request reducer looks like

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

and is composed in an index.ts file with other reducers

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

and the StoreModule is imported with the reducers map like this

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

Do you have any idea what's going on? Thanks and cheers!


回答1:


You need to wrap your reducer as function call like this:

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);
}

See the official doc -

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



来源:https://stackoverflow.com/questions/56993101/error-building-angularngrx-8-for-production-when-using-createreducer-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!