将大型异径管拆分为较小的异径管 [英] Splitting big reducer into smaller reducers
问题描述
我有一个称为动物的特征缩减器(切片缩减器).我想将这些减速器拆分为哺乳动物,鸟类,鱼类等.这部分很容易,因为我可以简单地使用ActionReducerMap
.
I have a feature reducer (slice reducer) called animals. I want to split these reducers to mammals, birds, fishes etc. This part is easy as I can simply use the ActionReducerMap
.
现在让我们说哺乳动物的减速器状态非常庞大,我想将其拆分为几个较小的减速器,即猫科,狗科等.ActionReducerMap不会返回减速器且不可嵌套.我尝试在网上搜索解决方案或示例,但找不到.简而言之,我的问题是如何制作多层嵌套的减速器.
Now let's say the mammals' reducer's state is huge and I want to split it to several smaller reducers i.e cat's family, dog's family etc. the ActionReducerMap is not returning a reducer and is not nestable. I tried searching the web for solution or example but I couldn't find. My question, in short, is how to make multi-level nested reducers.
export interface AnimalsState{
mammals: fromMammals.mammalsState;
birds: fromBirds.birdsState;
}
export const reducers: ActionReducerMap<AnimalsState> = {
mammals: fromMammals.reducer,
birds: fromBirds.reducer
};
我想将哺乳动物的减速器分成较小的减速器.
I want to split mammals reducer into smaller reducers.
推荐答案
You can you can compose a new reducer using the combineReducers function from @ngrx/store
, this will allow you to combine your dogs
and cats
reducers for the mammals
state.
我举了一个简单的示例,说明如何在 stackblitz .
I made a quick example of how to use it on stackblitz.
combineReducers
函数的示例可以在 app/store/mammals/mammals.reducer.ts
:
The example of the combineReducers
function can be found in app/store/mammals/mammals.reducer.ts
:
import { combineReducers } from '@ngrx/store';
import { catsStoreName, catsReducer, CatsState } from '../cats';
import { dogsStoreName, dogsReducer, DogsState } from '../dogs';
export type MammalsState = {
[catsStoreName]: CatsState,
[dogsStoreName]: DogsState,
}
export const mammalsReducer = combineReducers<MammalsState>({
[catsStoreName]: catsReducer,
[dogsStoreName]: dogsReducer,
});
这篇关于将大型异径管拆分为较小的异径管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!