我正在使用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 {}
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);
};
我的减速器是标准的减速器,没什么特别的。
来自React / Redux背景,我会像这样设置多个reducers,但是在Angular中,当我尝试从商店中选择时,我得到了未定义。当我尝试使用开发工具查看商店时,我看不到我的简化器和状态都只是{}
如何在Angular 6 / NgRX 4中设置多个Reducer?
答案 0 :(得分:4)
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,
};
答案 1 :(得分:0)
对我来说,这个博客帮助了: https://blog.strongbrew.io/combinereducers-enhanced/
试试这个:
export interface IAppState {
[counterKey]: NestedType;
[profileKey]: any;
}
export interface NestedType {
[nestedKey1]: any;
[nestedKey2]: any;
}
export const reducers: ActionReducerMap<IAppState> = {
[counterKey]: combineReducers({
[nestedKey1]: nestedKey1Reducer;
[nestedKey2]: nestedKey2Reducer;
}),
[profileKey]: profileReducer,
};