【问题标题】:How to access ngrx entity selectors in angular lazy-loading module?如何在角度延迟加载模块中访问ngrx实体选择器?
【发布时间】:2020-08-15 05:20:00
【问题描述】:

我正在尝试使用 NGRX 状态管理库来实现应用程序。我能够创建操作和减速器来将数据推送到延迟加载状态。但是我正在努力实现选择器以将数据获取到组件。这就是我到目前为止所做的事情

reducer/job-file.reducer.ts这里我使用的是ngrx实体插件

import {Action, createReducer, on} from '@ngrx/store';

import * as JobFileActions from '../actions';
import {JobFile} from '../../models/job-file.model';
import {createEntityAdapter, EntityAdapter, EntityState} from '@ngrx/entity';

export const jobFIleFeatureKey = 'jobFile';

export const adapter: EntityAdapter<JobFile> = createEntityAdapter<JobFile>({
  selectId: (jobFile: JobFile) => jobFile.jobRefId
});

export interface State extends EntityState<JobFile> {
  selectedJobRefId: string;
}

export const initialState: State = adapter.getInitialState({
  selectedJobRefId: null,
});

export const reducer = createReducer(
  initialState,
  on(JobFileActions.AddJobFile as any, (state: State, action: {jobFile: JobFile}) => {
    return adapter.addOne(action.jobFile, state);
  })
);

export const selectedJobRefId = (state: State) => state.selectedJobRefId;

reducer/index.ts

import {ActionReducerMap } from '@ngrx/store';
import * as fromJobFile from './job-file.reducer';

export const scheduleFeatureKey = 'schedule';

export interface ScheduleState {
  [fromJobFile.jobFIleFeatureKey]: fromJobFile.State;
}

export const reducers: ActionReducerMap<ScheduleState> = {
  [fromJobFile.jobFIleFeatureKey]: fromJobFile.reducer
};

schedule.module.ts

import * as fromSchedule from './store/reducers';
@NgModule({
  declarations: [ScheduleComponent, ContainerDetailsComponent, AssignScheduleComponent, LegComponent, ResourceOverviewPanelComponent,
    ResourceNavigationComponent],
  imports: [
    SharedModule,
    ScheduleRoutingModule,
    StoreModule.forFeature('schedule', fromSchedule.reducers)
  ]
})

selectors.ts这是我现在苦苦挣扎的地方

import { adapter as jobFileAdaptor } from '../reducers/job-file.reducer';
import {createFeatureSelector, createSelector} from '@ngrx/store';
import { ScheduleState } from '../reducers';

export const selectJobFileState = createFeatureSelector<ScheduleState>('jobList');

export const a = createSelector(selectJobFileState, jobFileAdaptor.getSelectors().selectAll);
export const {
  selectIds: selectAllJobIds,
  selectAll: selectAllJobFiles,
  selectEntities: selectAllJobEntities,
  selectTotal: selectTotalJobs
}  = jobFileAdaptor.getSelectors();

我遇到了错误。有谁知道如何编写这些选择器

【问题讨论】:

    标签: angular typescript ngrx ngrx-entity


    【解决方案1】:

    看起来问题出在selectJobFileState 的类型上,因为ScheduleState 不是EntityState 的实现。相反,它包含一个以 EnitityState 作为其值的键。

    // In your reducers index.ts
    export { State as JobFileEntityState } from './job-file.reducer';
    
    // In selectors.ts
    import { JobFileEntityState } from '../reducers';
    
    export const selectJobFileState = createFeatureSelector<JobFileEntityState>('jobList');
    

    旁注:如果您要将功能选择器键指定为selectors.ts 中的字符串文字,为什么还要在其他地方使用变量。始终如一。您可能应该导入 jobList 功能键变量并使用它而不是字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多