Angular 6/NGRX组合减速器

Har*_*lue 6 redux ngrx angular

我正在使用Angular 6 w/NgRX 4.我有多个我想要结合的减速器.

app.module.ts

  import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';

    import { StoreModule } from '@ngrx/store';
    import { EffectsModule } from '@ngrx/effects';

    import { StoreDevtoolsModule } from '@ngrx/store-devtools';

    import { AppComponent } from './app.component';
    import counterEffects from './store/counter/counter.effects';

    import reducers from './store/reducers';

    @NgModule({
      declarations: [AppComponent],
      imports: [
        BrowserModule,
        StoreModule.forRoot(reducers),
        EffectsModule.forRoot([counterEffects]),
        StoreDevtoolsModule.instrument({
          maxAge: 10,
        }),
      ],
      providers: [],
      bootstrap: [AppComponent],
    })
    export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

reducers.ts

import { combineReducers } from '@ngrx/store';
import { reducer as counterReducer, key as counterKey } from './counter';
import { reducer as profileReducer, key as profileKey } from './profile';

const appReducer = combineReducers({
  [counterKey]: counterReducer,
  [profileKey]: profileReducer,
});

export default (state, action) => {
  if (action.type === 'REDIRECT_TO_EXTERNAL') {
    state = undefined;
  }

  return appReducer(state, action);
};
Run Code Online (Sandbox Code Playgroud)

我的减速机是标准的减速机,没什么特别的.

来自React/Redux背景,我会像这样设置多个reducers,但是在Angular中,当我尝试从商店中选择时,我得到了未定义.当我尝试使用开发工具查看商店时,我看不到我的减速器和状态都不简单{}

如何在Angular 6/NgRX 4中设置多个减速器?

Har*_*lue 5

import { ActionReducerMap } from '@ngrx/store';
import { reducer as counterReducer, key as counterKey } from './counter';
import { reducer as profileReducer, key as profileKey } from './profile';

export interface IAppState {
  [counterKey]: any;
  [profileKey]: any;
}

export const reducers: ActionReducerMap<IAppState> = {
  [counterKey]: counterReducer,
  [profileKey]: profileReducer,
};
Run Code Online (Sandbox Code Playgroud)

  • 是否可以嵌套ActionReducerMap?假设我希望减速器成为其他更大的减速器的一部分,那怎么办呢? (10认同)