如何在angular懒加载模块中访问ngrx实体选择器?

问题描述 投票:0回答:1

我试图使用NGRX状态管理库来实现一个应用程序,我能够创建动作和reducers来推送数据到懒加载状态。但我正在努力实现选择器来获取数据到组件。这是我目前所做的

reducerjob-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;

reducerindex.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)
  ]
})

选择器.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();

我得到了下面的错误。有人知道如何写这些选择器吗?enter image description here

angular typescript ngrx ngrx-entity
1个回答
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特征键变量,并使用它来代替字符串。

© www.soinside.com 2019 - 2024. All rights reserved.