Angular 存储状态未定义
Angular store state undefined
我有一个 Angular 7 应用程序,它使用带有实体的 ngrx-store。有一个问题是我的store state的元素在我的effect中是未定义的。
这是效果的片段:
@Effect()
loadAccountSummaries$ = this.action$.pipe(
ofType(fromAccountSummary.LOAD_ACCOUNT_SUMMARIES),
withLatestFrom(this.store),
filter(([ action, storeState ]: [ Action, State ]) => {
// storeState.accountSummaries is undefined
return !(storeState.accountSummaries.loaded || storeState.accountSummaries.loading)
}
),
switchMap(() => {
this.store.dispatch(new LoadingAccountSummaries());
return this.accountSummaryService.loadAccountSummaries()
.pipe(map(accountSummaries => {
this.store.dispatch(new LoadingAccountSummaries());
return new fromAccountSummary.LoadAccountSummariesSuccess(accountSummaries);
}));
}));
我的reduer包括以下内容:
export interface AccountSummariesState extends EntityState<AccountSummary> {
loading: boolean;
loaded: boolean;
}
初始状态是这样的:
export const initialAccountSummariesState: AccountSummariesState = adapter.getInitialState({
loading: false,
loaded: false
});
减速器初始化如下:
export function reducer(
state = initialAccountSummariesState,
action: fromAccountSummary.AccountSummaryActionsUnion
) {
// ...
}
state 和 reducer 是这样注册的:
export interface State {
accountSummaries: fromAccountSummary.AccountSummariesState;
contributionDetails: fromContributionDetail.ContributionDetailState;
}
export const reducers: ActionReducerMap<State> = {
accountSummaries: fromAccountSummary.reducer,
contributionDetails: fromContributionDetail.reducer
};
@NgModule({
imports: [
// ...
StoreModule.forFeature('accountSummary', reducers.accountSummaries),
StoreModule.forFeature('contributionDetail', reducers.contributionDetails),
EffectsModule.forFeature([ AccountSummaryEffects, ContributionDetailEffects ])
// ...
]
})
为什么我的 storeState.accountSummaries
在我的效果中未定义?据我所知,我已经正确注册并初始化了所有内容。
在 ngrx 模块中,您正在创建 accountSummary
功能,但您调用了 accountSummaries
键。尝试将调用更改为 storeState.accountSummary
我有一个 Angular 7 应用程序,它使用带有实体的 ngrx-store。有一个问题是我的store state的元素在我的effect中是未定义的。
这是效果的片段:
@Effect()
loadAccountSummaries$ = this.action$.pipe(
ofType(fromAccountSummary.LOAD_ACCOUNT_SUMMARIES),
withLatestFrom(this.store),
filter(([ action, storeState ]: [ Action, State ]) => {
// storeState.accountSummaries is undefined
return !(storeState.accountSummaries.loaded || storeState.accountSummaries.loading)
}
),
switchMap(() => {
this.store.dispatch(new LoadingAccountSummaries());
return this.accountSummaryService.loadAccountSummaries()
.pipe(map(accountSummaries => {
this.store.dispatch(new LoadingAccountSummaries());
return new fromAccountSummary.LoadAccountSummariesSuccess(accountSummaries);
}));
}));
我的reduer包括以下内容:
export interface AccountSummariesState extends EntityState<AccountSummary> {
loading: boolean;
loaded: boolean;
}
初始状态是这样的:
export const initialAccountSummariesState: AccountSummariesState = adapter.getInitialState({
loading: false,
loaded: false
});
减速器初始化如下:
export function reducer(
state = initialAccountSummariesState,
action: fromAccountSummary.AccountSummaryActionsUnion
) {
// ...
}
state 和 reducer 是这样注册的:
export interface State {
accountSummaries: fromAccountSummary.AccountSummariesState;
contributionDetails: fromContributionDetail.ContributionDetailState;
}
export const reducers: ActionReducerMap<State> = {
accountSummaries: fromAccountSummary.reducer,
contributionDetails: fromContributionDetail.reducer
};
@NgModule({
imports: [
// ...
StoreModule.forFeature('accountSummary', reducers.accountSummaries),
StoreModule.forFeature('contributionDetail', reducers.contributionDetails),
EffectsModule.forFeature([ AccountSummaryEffects, ContributionDetailEffects ])
// ...
]
})
为什么我的 storeState.accountSummaries
在我的效果中未定义?据我所知,我已经正确注册并初始化了所有内容。
在 ngrx 模块中,您正在创建 accountSummary
功能,但您调用了 accountSummaries
键。尝试将调用更改为 storeState.accountSummary