请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Cannot read property 'ids' of undefined

我对照该项目angular5 的版本改写;我需要用到一个数组(address),循环遍历它时报错:Cannot read property ‘ids’ of undefined,我用了ngrx/entity;
运行的到的address 状态:
addressList(pin):{
ids(pin):[‘1’,‘2’,‘3’],
entities(pin):{
1(pin): {id:1 ,desc: ‘A楼’}
2(pin): {id:2 ,desc: ‘B楼’}
3(pin): {id:3 ,desc: ‘C楼’}
}
slectedId(pin): null
}
部分代码如下:
action
export class LoadingAddressSuccessAction implements Action {
readonly type = LOAD_ADDRESS_SUCCESS;
constructor(public payload: AddressTree[]) { }
}
reducer:
export interface State extends EntityState {
selectedId: string | null;
}

export const adapter: EntityAdapter = createEntityAdapter({
sortComparer: false
});
export const initialState: State = adapter.getInitialState({
selectedId: null
});
export function reducer(state = initialState, action: actions.Actions): State {
switch (action.type) {
case actions.LOAD_ADDRESS_SUCCESS: {
return {
…adapter.addMany(action.payload, state),selectedId: null
}
}
case actions.LOAD_ADDRESS_FAIL:
default: {
return state;
}
}
}
export const getSelectedId=(state: State)=> state.selectedId;
reducer的index文件:
export const getAddressState = createFeatureSelector<fromAddress.State>(‘addressTree’);
export const {
selectIds: getAddressIds,
selectEntities: getAddressEntities,
selectAll: getAddress,
selectTotal: getAddressTotal
} = fromAddress.adapter.getSelectors(getAddressState);

正在回答 回答被采纳积分+3

2回答

提问者 风_863495732 2018-10-09 13:40:12

已解决,有个类名写错了


0 回复 有任何疑惑可以回复我~
提问者 风_863495732 2018-09-30 17:27:53

constructor(

private store$: Store<fromRoot.State>,

private modalService: NzModalService) {

this.store$.dispatch(new actions.LoadingAddressAction('1'));

this.addressList$ = this.store$.pipe(select(fromRoot.getAddress));

this.addressList$.subscribe(res=>{

console.log('地址结果:',res)

})

}

this.addressList$ 流subscribe 或async 后报错Cannot read property ‘ids’ of undefined


0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信