我在Ionic / Angular应用程序中获取组件以订阅@ngrx商店的 part 时遇到了很多麻烦。
在我的应用中,我有一个index.ts文件,其中包含:
import * as fromReducer1 from './reducer1';
import * as fromReducer2 from './reducer2';
export interface ApplicationState {
reducer1: fromReducer1.Reducer1State;
reducer2: fromReducer2.Reducer2State;
}
在reducer1中我有:
export interface Reducer1State {
products {
items: any[],
prices: any[]
},
filters: any[]
}
和导出功能
export function Reducer1 etc
在我的组件中,我可以通过这样做很高兴地订阅Reducer 1:
this.store.select('Reducer1')
.subscribe((store: any) => {})
这样做没问题,但我想要做的是让我的组件订阅Reducer1,但 ONLY 选择Products对象的更改,即如果过滤器对象发生更改,那么我对它不感兴趣。无论是或者至少在组件订阅时,它都可以检测产品对象是否与上次相同,即使用DistinctUntilChanged(我无法使其工作)
我该怎么做(我已经搜索并尝试了几件事,但没有设法让任何事情发挥作用)。谢谢!
答案 0 :(得分:1)
你可以写内部选择器
// take the whole state reducer
export const reducer2State= (state: State) => state.transactions;
//take inner reducer
export const getInnerState = createSelector(reducer2State,
fromReducer2.something);
// select in component
this.innerReducer = this.store.select(fromRoot.getInnerState );
如果您正在使用ngrx2结帐example app。 如果您正在使用ngrx4结帐此example app