本文介绍了使用createReducer函数构建用于生产的angular + ngrx 8时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

在开发模式下根本没有问题.

In development mode there is no problem at all.

请求 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 文件

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

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

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

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!

推荐答案

您需要将reducer包装为函数调用,如下所示:

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

查看官方文档-

https://ngrx.io/guide/store/reducers#创建减少器功能

这篇关于使用createReducer函数构建用于生产的angular + ngrx 8时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:22