鉴于以下情况(假设我们不能改变状态的结构):
StoreModule.forRoot({
a: aReducer,
b: {
b1: b1Reducer,
b2: b2Reducer
}
});
并且b1Reducer
取决于 的值a
(例如,因为它包含诸如用户信息之类的内容)。访问(只读)的最惯用方式是a
什么b1Reducer
?
我想出的解决方案是使用@ngrx/effects
, dispatch 另一个a
可以在减速器中使用的动作:
@Effect()
augmentAction$ = this.action$
.ofType(Actions.Action1)
.withLatestFrom(this.store$)
.switchMap(([action, state]:[Action, AppState]) => {
const a = state.a;
return [new Actions.Action2(a)];
});
a
这是可行的,但是如果在许多 reducer 中使用几乎每个操作都需要重新调度,则变得难以管理。有没有更好的方法来处理这个?