【问题标题】:Ngrx selector undefined EntityAdapterNgrx 选择器未定义的 EntityAdapter
【发布时间】:2020-08-13 07:43:59
【问题描述】:

在这个问题上兜圈子。每次我似乎使用选择器时,它都是未定义的。我检查了其他帖子以及我已经拥有的他们缺少的代码。使用选择器时我一直不确定。我也有 ngrx devtools 设置。任何帮助将不胜感激。

组件:

import * as fromBusiness from '@app/@store/reducers/index';

    ngOnInit(): void {
    const id = this.params.id;
    this.business$ = this.store.pipe(select(fromBusiness.getBusinessEntityById(), { id }))
    .pipe(
      map((business: BusinessModel) => {
        return this.business = business;
      }),
    );
    console.log(this.business);
  }

功能模块

StoreModule.forFeature('businesses', fromBusiness.businessFuncReducer),

减速机:

export interface State extends EntityState<BusinessModel> {}

export const adapter: EntityAdapter<BusinessModel> = createEntityAdapter<
  BusinessModel
>( {
  selectId: (business: BusinessModel) => business.id,
});

export const initialState = adapter.getInitialState({});

export const businessReducer = createReducer(
  initialState,
  //reducer methods here, upsertone, addone etc
);

export function businessFuncReducer(state: State | undefined, action: Action) {
  return businessReducer(state, action);
}

export const businessFeatureSelector = createFeatureSelector<State>(
  'businesses'
);

export const {
  selectIds,
  selectEntities,
  selectAll,
  selectTotal,
} = adapter.getSelectors();


export const getAllBusinesses = createSelector(
  businessFeatureSelector,
  selectAll
);

reducer.index.ts

export interface State {
  business: fromBusiness.State;
}

export const selectBusinessState = createFeatureSelector<fromBusiness.State>(
  'businesses'
);

export const selectBusinessEntities = createSelector(
  selectBusinessState,
  fromBusiness.selectEntities
);

export const getBusinessEntityById = () => {
  return createSelector(
    selectBusinessEntities,
    (entities: Dictionary<BusinessModel>, props: {id: string}) => {
      return entities[props.id];
    },
  );
};

感谢您对此的任何帮助。花了2天没有解决方案。附上 devtools 树的截图。

【问题讨论】:

    标签: angular ngrx


    【解决方案1】:

    我建议将导入量加倍并确保它是正确的。问题可能不在于正在使用的选择器,而可能是父选择器之一?

    【讨论】:

    • 我尝试了父级别,但无论我尝试什么选择器,我总是不确定。我想知道应用程序模块是否设置正确。我有:StoreModule.forFeature('businesses', businessFuncReducer),
    【解决方案2】:

    原来,这不是选择器或模块。这就是我消费它的方式。周围的例子对我不起作用,所以我的做法略有不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      相关资源
      最近更新 更多