我有一个简单的状态:
export interface ItemsState{
items: Item[],
otherItem: OtherItem,
}
const initialState: ItemsState= {
items: [],
otherItem: {} as OtherItem,
}
还有一个选择器:
const getItemFeatureState = createFeatureSelector<ItemsState>('items');
export const getItemValue= createSelector(
getItemFeatureState,
state => state.otherItem,
);
在我的组件中,我使用选择器:
public item: Item = {} as Item;
constructor(private itemsStore: Store<fromItem.State>) {
this.itemsStore.pipe(
select(fromItem.getItemValue),
distinctUntilChanged(),
).subscribe(item=> this.item = item);
}
但是当我通过执行以下简单操作来更新此本地 Item
时:
this.item.value = someValue;
this.item.value
将在 ItemsState 中更新,无需使用任何操作。
这怎么可能?
最佳答案
这是因为该项目共享相同的引用。如果您更新它,您在商店中的商品也会更新。
这不是你想要的。要在开发过程中捕获这些“错误”,您可以使用包 ngrx-store-freeze以防止这种情况发生。
关于javascript - Angular 6 + Ngrx 状态值更新而不使用操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52875149/