NGRX - 结合选择器和 Prop

标签 ngrx ngrx-store

当其中一个需要 Prop 时,如何组合 reducer ?

我有以下型号:

interface Device {
  id: string;
  data: IDeviceData;
}

和 DeviceReducer 如下所示:
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { Device } from '../model/device';
import { SubnetBrowserApiActions } from 'src/app/explorer/actions';

export interface State extends EntityState<Device> { }

export const adapter: EntityAdapter<Device> = createEntityAdapter<Device>();

export const initialState: State = adapter.getInitialState();

export function reducer(
  state = initialState,
  action:
    | SubnetBrowserApiActions.SubnetBrowserApiActionsUnion
): State {
  switch (action.type) {
    case SubnetBrowserApiActions.SubnetBrowserApiActionTypes.LoadEntriesSucces: {
      return adapter.upsertMany(action.payload.entries, state);
    }

    default: {
      return state;
    }
  }
}

const {
  selectAll,
} = adapter.getSelectors();

export const getAllDevices = selectAll;

在我的另一个 reducer 中,当我想使用一组 id 选择设备时,我使用以下代码:
export const getVisibleDrives = createSelector(
  [fromRoot.getAllDevices, getVisibleDrivesSerialNumbers],
  (devices, visibleDevicesIds) =>
    devices.filter((device) => onlineDevicesIds.includes(device.serialNumber)),
);

这段代码非常重复,所以我想添加添加参数化选择器,这些选择器将只返回这些驱动器,这些驱动器在我作为 Prop 传递的数组中具有 id。我试图做的事情如下:

DeviceReduced 中的附加选择器
export const getDrivesWithIds = (ids: string[]) => createSelector(
  getAllDevices,
  devices => devices.filter(device => ids.includes(device.id))
);

然后将它们组合在 中关注 道路:
export const getVisibleDrives = createSelector(
  getVisibleDrivesSerialNumbers,
  (ids) => fromRoot.getDrivesWithIds
);

这里的问题是这个选择器的返回类型是
(ids: string[]) => MemoizedSelector<State, Device[]>
这使我无法对这个选择器做任何有用的事情。例如,我想按关键字过滤此列表,但我无法使用 filter方法:

示例用法
export const getFilteredVisibleDrives = createSelector(
  [getVisibleDrives, getKeywordFilterValue],
  (visibleDrives, keywordFilter) => {
    return visibleDrives
      .filter(drive => // in this line there is an error: Property 'filter' does not exist on type '(ids: string[]) => MemoizedSelector<State, Device[]>'
        drive.ipAddress.toLowerCase().includes(keywordFilter.toLowerCase()) ||
        drive.type.toLowerCase().includes(keywordFilter.toLowerCase()) ||
        drive.userText.toLowerCase().includes(keywordFilter.toLowerCase())
      );
  },
);

最佳答案

选择器:

export const getCount = () =>   
  createSelector(     
    (state, props) => state.counter[props.id],     
    (counter, props) => counter * props.multiply
  );

成分:
this.counter2 = this.store.pipe(
  select(fromRoot.getCount(), { id: 'counter2', multiply: 2 })
);   
this.counter4 = this.store.pipe(
  select(fromRoot.getCount(), { id: 'counter4', multiply: 4 })
);

看我的帖子NgRx: Parameterized selectors
了解更多信息。

关于NGRX - 结合选择器和 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54707140/

相关文章:

angular - 如何在 Angular 2 的 ngrx 效果中访问参数?

angular - NgRx/Effects 不触发 Reducer

javascript - @Ngrx/存储 : how to query models with relationships

angular - NGRX Effect 中的内存泄漏

javascript - 可观察的异步与同步

Angular NGRX - 为 Redux-Devtools 命名商店/应用程序实例

javascript - 本地存储和ngrx存储

angular - 参数化 ngrx 选择器的方法有什么区别

使用 ngxs/store @select 进行 Angular Testing 会给出错误 : "SelectFactory not connected to store!"