简体   繁体   English

如何通过 id 从 ngrx-entity 商店中选择唯一实体

[英]How to select a unique entity by id from the ngrx-entity store

I have in my ngrx-entity store the prsnls entity well described as array of objects as shown in redux devtool.我在我的 ngrx-entity 中存储了 prsnls 实体,它被很好地描述为对象数组,如 redux devtool 中所示。 在此处输入图像描述

I'm able to select all prsnl entities like this:我可以像这样选择所有 prsnl 实体:

export const selectAllPrsnls = createSelector(
    selectPrsnlState,
    prsnlsState => {
        const allPrsnls = Object.values(prsnlsState.entities)
        return allPrsnls;
    }
);

But I'm unable to get a unique prsnl by it's id.但是我无法通过它的 id 获得唯一的 prsnl。 This is what I did:这就是我所做的:

export const selectUniquePrsnls = (id:string) => createSelector(
    selectPrsnlState,
    prsnlsState => {
        const uniquePrsnls = Object.values(prsnlsState.entities[id])
        return uniquePrsnls;
    }
  );

The unique prsnl is not fetched because when I console.log after susbscribing to the createSelector observable, I get undefined in the console.唯一的 prsnl 没有被获取,因为当我在订阅 createSelector observable 后 console.log 时,我在控制台中得到未定义。 This is how I subscribed to it:这就是我订阅它的方式:

getUniquePersnlValues(id:string){
    this.store
    .pipe(select(selectUniquePrsnls), map((per) => per))
    .subscribe((value: any) => this.uniquePrsnl = value);
    console.log('prsnl', this.uniquePrsnl);
  }

I think I'm not able to troubleshoot this issue because I don't understand well what does the selectPrsnlState when creating the selector like on srrenshot:我认为我无法解决此问题,因为我不太了解在创建选择器时selectPrsnlState的作用,例如在 srrenshot 上: 在此处输入图像描述

How can I then get a unique prsnl from the prsnls entities in this situation.在这种情况下,如何从 prsnls 实体中获取唯一的 prsnl。 Any help is much appreciated.任何帮助深表感谢。 Thanks in advance.提前致谢。

You can get element by id like this:您可以像这样通过 id 获取元素:

export const selectUniquePrsnls = (id:string) => createSelector(
    selectPrsnlState,
    prsnlsState => prsnlsState.entities[id]
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM