我有两个功能,订单和物品。在订单功能中,我有一个调度动作的组件。
简而言之,它看起来像这样:
constructor(store: Store<OrdersStateContract>) {
this.orders_subs = store.pipe(select('orders')).subscribe( (orders) => {
this.orders = orders.orders;
});
this.store = store;
}
ngOnInit() : void {
this.store.dispatch( new LoadOrders({}) );
}
我意识到当我分派动作LoadOrders时,它会被项功能减少器所捕获。这是常见的行为吗?还是有什么问题?
orders.module.ts :
@NgModule({
declarations: [
],
imports : [
MaterialModule,
BrowserModule,
StoreModule.forFeature('orders', OrdersReducer),
EffectsModule.forFeature([OrdersEffects]),
],
providers : [
OrderService
]
})
items.module.ts :
@NgModule({
declarations: [
/*not important*/
],
imports : [
MaterialModule,
BrowserModule,
StoreModule.forFeature('items', ItemsReducer),
EffectsModule.forFeature([ItemsEffects])
]
})
items.reducer.ts
export const ItemsReducer = (state: ItemsStateContract = initialState,
action: ItemActions) => {
switch (action.type) {
case ItemActionTypes.ItemsFromOrderLoadSuccess:
return adapter.addAll(action.payload.items, state);
default:
return state;
}
};
orders.reducer.ts
export const OrdersReducer = (state: OrdersStateContract = initialState,
action: OrderActions) => {
switch (action.type) {
case OrderActionTypes.LoadOrders: {
return adapter.addAll(action.payload.orders, state);
}
default: {
return state;
}
}
};