Angular 错误类型错误:无法读取属性';数据';减速器中未定义的参数
我得到以下错误: 错误类型错误:无法读取未定义的属性“数据” 缩减器文件:Angular 错误类型错误:无法读取属性';数据';减速器中未定义的参数,angular,typescript,redux,ngrx,angular8,Angular,Typescript,Redux,Ngrx,Angular8,我得到以下错误: 错误类型错误:无法读取未定义的属性“数据” 缩减器文件: import * as PfaAction from '../actions/profilefunctionarea.actions'; import { Action, createSelector, createFeatureSelector } from "@ngrx/store"; import { AppAction } from 'src/app/app.action'; import { Pfa } fro
import * as PfaAction from '../actions/profilefunctionarea.actions';
import { Action, createSelector, createFeatureSelector } from "@ngrx/store";
import { AppAction } from 'src/app/app.action';
import { Pfa } from '../shared/pfa';
export interface State {
data: Pfa[];
}
const initialState: State = {
data: [],
};
export function reducer(state = initialState, action: AppAction) {
switch(action.type) {
case PfaAction.GET_PROFILEFUNCTIONAREA:
return {
...state,
action: PfaAction.GET_PROFILEFUNCTIONAREA
}
}
}
export const getPfaState = createFeatureSelector < State > ('pfas');
export const getAllPfas = createSelector(getPfaState, (state: State) => state.data); <-- here getting error
动作。ts:
import { Pfa } from 'src/app/ngrx/shared/pfa';
pfas: Observable<Pfa[]>;
ngOnInit() {
this.pfas = this.store.select(getAllPfas);
}
<select *ngFor="let pfa of pfas | async">
<option value="pfa.id">{{pfa.area}}</option>
</select>
export class Pfa {
id: number;
area: string;
}
import { Action } from '@ngrx/store';
import { ProfileFunctionArea } from 'src/app/_interface/profilefucntionarea.module';
import { Pfa } from '../shared/pfa';
export const GET_PROFILEFUNCTIONAREA = '[ALL] Profile Function Area';
export const GET_PROFILEFUNCTIONAREA_SUCCESS = '[ALL] Profile Function Area Success';
export const GET_PROFILEFUNCTIONAREA_ERROR = '[ALL] Profile Function Area Error';
export class GetProfileFunctionArea implements Action {
readonly type = GET_PROFILEFUNCTIONAREA;
}
export class GetProfileFunctionAreaSuccess implements Action {
readonly type = GET_PROFILEFUNCTIONAREA_SUCCESS;
constructor(public payload: Pfa[]) { }
}
export class GetProfileFunctionAreaError implements Action {
readonly type = GET_PROFILEFUNCTIONAREA_ERROR;
constructor(public payload: Error) { }
}
@Injectable()
export class ProfileFunctionAreaEffects {
constructor(private actions$: Actions, private ps: ProfileService) { }
@Effect() GetProfileFunctionArea$: Observable<Action> = this.actions$.pipe( ofType(PfaAction.GET_PROFILEFUNCTIONAREA),
switchMap(() => this.ps.GetProfileFunctionAreaGet()),
map(items => new GetProfileFunctionAreaSuccess(items)), <-- getting error here
catchError((err) => [new GetProfileFunctionAreaError(err)]));
}
效果。ts:
import { Pfa } from 'src/app/ngrx/shared/pfa';
pfas: Observable<Pfa[]>;
ngOnInit() {
this.pfas = this.store.select(getAllPfas);
}
<select *ngFor="let pfa of pfas | async">
<option value="pfa.id">{{pfa.area}}</option>
</select>
export class Pfa {
id: number;
area: string;
}
import { Action } from '@ngrx/store';
import { ProfileFunctionArea } from 'src/app/_interface/profilefucntionarea.module';
import { Pfa } from '../shared/pfa';
export const GET_PROFILEFUNCTIONAREA = '[ALL] Profile Function Area';
export const GET_PROFILEFUNCTIONAREA_SUCCESS = '[ALL] Profile Function Area Success';
export const GET_PROFILEFUNCTIONAREA_ERROR = '[ALL] Profile Function Area Error';
export class GetProfileFunctionArea implements Action {
readonly type = GET_PROFILEFUNCTIONAREA;
}
export class GetProfileFunctionAreaSuccess implements Action {
readonly type = GET_PROFILEFUNCTIONAREA_SUCCESS;
constructor(public payload: Pfa[]) { }
}
export class GetProfileFunctionAreaError implements Action {
readonly type = GET_PROFILEFUNCTIONAREA_ERROR;
constructor(public payload: Error) { }
}
@Injectable()
export class ProfileFunctionAreaEffects {
constructor(private actions$: Actions, private ps: ProfileService) { }
@Effect() GetProfileFunctionArea$: Observable<Action> = this.actions$.pipe( ofType(PfaAction.GET_PROFILEFUNCTIONAREA),
switchMap(() => this.ps.GetProfileFunctionAreaGet()),
map(items => new GetProfileFunctionAreaSuccess(items)), <-- getting error here
catchError((err) => [new GetProfileFunctionAreaError(err)]));
}
@Injectable()
导出类ProfileFunctionalEffects{
构造函数(私有操作$:操作,私有ps:ProfileService){}
@Effect()GetProfileFunctionArea$:Observable=this.actions$.pipe(类型为pfaaaction.GET\u PROFILEFUNCTIONAREA),
switchMap(()=>this.ps.getProfileFunctionReaget()),
映射(items=>newgetprofilefunctionreasuccess(items)),[newgetprofilefunctionreaerror(err)];
}
这行代码是错误的
return {
...state,
action: PfaAction.GET_PROFILEFUNCTIONAREA
}
你应该回来
return {
...state,
data: [] // add your array here
}
reducer应始终返回状态,在代码段中,reducer将仅返回
PFAAAction.GET_PROFILEFUNCTIONAREA
的状态,并将返回undefined
的其他每一个分派操作。这是因为一个动作将被分派到所有减速器
要解决此问题,请添加一个默认值
案例:
export function reducer(state = initialState, action: AppAction) {
switch(action.type) {
case PfaAction.GET_PROFILEFUNCTIONAREA:
return {
...state,
action: PfaAction.GET_PROFILEFUNCTIONAREA
}
// add this
default:
return state;
}
}
能否请您添加一些更多信息,如错误出现的位置、您试图实现的目标等。除此之外,我还没有使用ngrx等工具,但我看到了
。数据可在reducer filecreateSelector
函数中访问。因此,状态
可能未定义。但,这个问题必须更详细,以便有人回答。@kiranghule27在试图获取component.html文件中的列表时,我得到了那个错误!让我试试这个。哪个数组?Pfa[]数据数组来自您的有效负载。你知道redux是如何工作的吗?我试图理解但没有得到它添加的两个文件代码,但是我在effects.ts
中得到错误,这一行map(items=>new getProfileFunctionarAsccess(items)),
用于items