Select 存储中的对象(按存储 NgRx 中的 id)

Select object from store by id from store NgRx

我如何根据我所在州的 selectedBuildingId 属性 select 来自我所在州的 Building

我的店铺:

export interface State extends EntityState<Building> {
    selectedBuildingId: string
}

是否可以有一些 select 或与此类似的东西,而不是将 id 传递给它,它只需要商店中的那个?

export const selectById = (id: string) 
    => createSelector(selectEntities, (buildingsDic) => buildingsDic[id]);

我的解决方案是只从商店获取 id,然后使用该 id 获取 Building,但我觉得这不是好的解决方案。

假设您有一个名为 selectBuildingId 的 selectedBuildingId 选择器,您可以在另一个选择器中使用一个选择器。

export const selectById = createSelector(selectEntities, selectBuildingId, (buildingsDic, id) => buildingsDic[id]);

看看NGRX doc