gpt4 book ai didi

javascript - 为什么这个选择器的投影仪函数返回未定义?

转载 作者:行者123 更新时间:2023-12-02 23:18:19 25 4
gpt4 key购买 nike

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

问题是投影仪函数未定义传入,我不明白为什么了。

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

最佳答案

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

从 ChartModule(定义功能状态的地方)内部访问选择器工作正常。

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

感谢@Jota.Toledo

关于javascript - 为什么这个选择器的投影仪函数返回未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57070257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com