簡體   English   中英

如何在 TS 代碼中從 NGRX Store 獲取 Value 而不是 Observable

[英]How to get Value instead of Observable from NGRX Store in TS code

考慮以下代碼

user$ = this._store.pipe(select(UserSelectors.selectUser));

現在我們有了一個可以像這樣使用的 Observable:

  1. 在視圖中:
(user$ | async)?.name
  1. 在組件 (TS) 中:
   let storeValue: IUser;
   this.user$.pipe(take(1)).subscribe(value => storeValue = value);

所以在我的組件中,我實現了一個 getter:

    protected get user(): IUser {
        let storeValue: IUser;
        this.user$.pipe(take(1)).subscribe(value => storeValue = value);
        return storeValue;
    }

我想知道是否有辦法使用選擇器返回實際值,例如:

user = this._store.VALUE(select(UserSelectors.selectUser));

所以我所有的getter邏輯都得到了照顧。

這是不可能的。 我什至會說這是一種反模式。

NgRx 是一個 Redux 實現,專為反應式編程而設計。

反應式:

  • 事件發生
  • 觸發動作,改變商店
  • 可以觸發效果,讀取/寫入數據並創建操作(更改存儲)
  • 視圖將使用 observable 消耗存儲。 每次 store 更改時,Oberservable 都會更新視圖。 所以店鋪的每一次變化都會轉發到視圖中。

您的方式不是被動的,這就是為什么它不適用於 NgRx。 您可以在沒有商店的情況下完成,或者您必須找到一種被動的方式來解決您的問題。

暫無
暫無

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

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