在@ngrx/store 4.0中提供root reducer

Nic*_*icu 7 ngrx ngrx-store ngrx-store-4.0

在@ngrx/store 2.0中,我们可以将根reducer作为函数提供,然后我们在应用程序中拆分逻辑.在我更新到@ngrx/store 4.0之后,我无法再使用此功能,我可以看到reducers需要是reducers的映射,它将在状态下的相同键下创建对象.有没有办法在@ngrx/store 4.0中使用旧的行为在我的状态中,组件知道另一个,我需要能够动态地分割我的状态,我还需要能够将动作分配到我的正确的reducer中自己的方式.app也被分成多个延迟加载的路由,在某些情况下重用来自另一个功能的数据.

 StoreModule.provideStore(reducer, {
      auth: {
        loggedIn: true
      }
    })

StoreModule.forRoot(reducers, {
      initialState: {
        auth: {
          loggedIn: true
        }
      }
    })
Run Code Online (Sandbox Code Playgroud)

我需要reducers成为一个获取完整状态并将其发送到正确的reducer的函数,有没有办法实现这种行为?

Nic*_*icu 4

在我第二次查看 ngrx repo 后,我明白了。为了达到想要的结果,我们需要用新的实现替换 @ngrx/store 减速器工厂。我注入了一个新的减速器工厂,现在应用程序像以前一样工作。关于如何替换减速器工厂的简单代码示例。

// This factory replaces @ngrx combine reducers so we can manage how we split the keys inside the state
export function combineReducersFactory(
    reducers: any,
    initialState: any = {}
): ActionReducer<any, Action> {
    return function combination(state = initialState, action) {
        const nextState: any = reducers(state, action);
        return nextState !== state ? nextState : state;
    };
}

export const NG_RX_STORE_PROVIDER = [
    StoreModule.forRoot(rootReducer, createEmptyState()),
];

export const NG_RX_REDUCER_FACTORY = [
    {
        provide: REDUCER_FACTORY,
        useFactory: () => combineReducersFactory
    }
];

@NgModule({
    imports: [
        ...NG_RX_STORE_PROVIDER
    ],
    declarations: [...APP_COMPONENTS, ...AG_GRID_COMPONENTS],
    providers: [...NG_RX_REDUCER_FACTORY]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)