簡體   English   中英

Angular 6 + Ngrx狀態值更新,無需使用操作

[英]Angular 6 + Ngrx state value update without using action

我有一個簡單的狀態:

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來防止這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM