问题描述
我在Ngrx中拥有一个布尔数据状态:
export interface MyState {
success?: boolean;
}
const initialState: MyState = { };
export const success = createSelector(myFeatureSelector,state => state.success);
因此,如果我执行以下操作:
myStateStore.select(success).subscribe(success => {
// this will only emit if the state changes
});
所以说,如果我依次调度2个动作,并且两个动作都将状态设置为true
,则只有第一个动作会发出成功值,而第二个动作将不会成功,因为该值未更改。有没有办法发出信号?
解决方法
您可能想将changeCount
属性添加到您的状态(每次更改(或成功)都会增加该属性
export interface MyState {
success?: boolean;
changeCount: number;
}
然后按如下所示更改代码:
this.myStateStore
.pipe(
select(changeCount),mergeMapTo(this.myStateStore.select(success))
)
.subscribe(success => {
});
,
重要的是要理解为什么selectors
不返回值。选择器为Memoized
,如果值不变,则不执行。
有一种方法可以跳过memoization
myStateStore.select(success).subscribe(success => {
// this will only emit if the state changes
});
success.release(); // This will force the selector to not memoized.