为什么此选择器的投影仪功能返回未定义?

时间:2019-07-17 07:03:25

标签: javascript angular ngrx

我正在尝试创建一个选择器,该选择器返回功能选择器的一部分。 状态通过效果异步填充,但从空数组开始。

问题是放映机功能传入的是不确定的,我不明白为什么了。

selector.ts

import { createFeatureSelector, createSelector } from '@ngrx/store';

import { ChartState } from '../reducers/chart.reducer';

export const selectChartState = createFeatureSelector<ChartState>('chart');

export const selectFrameworksState = createSelector(
  selectChartState,
  (chart: ChartState) => chart.frameworks // <= here chart is undefined??
);

reducer.ts

import { Framework } from 'src/app/_interfaces/framework.interface';

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

import * as ChartActions from '../actions/chart.actions';

export interface ChartState {
  frameworks: Framework[];
}

export const initialState: ChartState = {
  frameworks: []
};

export const chartReducer = createReducer(
  initialState,

  on(ChartActions.loadCharts, state => state),
  on(ChartActions.loadChartsSuccess, (state, action) => {
    return {...state, frameworks: [...action.data]};
  }),
  on(ChartActions.loadChartsFailure, (state) => state),

);

export function reducer(state: ChartState | undefined, action: Action) {
  return chartReducer(state, action);
}

module.ts

@NgModule({
  declarations: [
    ...  
  ],
  imports: [
    ...
    StoreModule.forFeature('chart', reducer),
    EffectsModule.forRoot([ChartEffects]) // forFeature() not working? Action provider?
  ],
  providers: [
    ...
  ]
})
export class ChartModule { }

app.component.ts

foo$ = this.store.pipe(select(selectFrameworksState));

1 个答案:

答案 0 :(得分:0)

在初始化状态的延迟加载特征切片之前,将调用选择器。

从ChartModule(定义了功能状态)中进行访问,选择器可以正常工作。

还建议解耦状态,仅在其模块内使用它。如果整个应用程序都需要状态,最好将其移至根状态。

感谢@ Jota.Toledo