我有一个名为doctors.list.component.ts的组件,它呈现一个医生列表。我使用@ngrx/effect从API获得了这个医生。我想在这个列表中使用ng-modal添加一个新的医生。流程是,当用户选择医生表单ng-modal时,必须将其添加到现有的医生列表中。为此,我编写了新的action和reducer。这个reducer抛出error object null is not iterable
这是我的doctorlist.action.ts
import { createAction, props } from '@ngrx/store';
import { Doctors } from 'src/app/admin/models/doctorProfileAdmin'
export const loadDoctors = createAction('[Doctors] Doctors List Request', props<{ hospitalId: string }>())
export const loadDoctorsSuccess = createAction('[Doctors] Doctors List Load Success', props<{ doctors: Doctors[] }>())
export const loadDoctorsFail = createAction('[Doctors] Doctors List Load Fail', props<{ errorMessage: string }>())
export const addExistingDoctorToList = createAction('[Doctors] Doctors List Load Success', props<{ doctor: Doctors }>())在我的doctor.list.reducer.ts
import { createReducer, on, Action } from '@ngrx/store';
import { DoctorListState } from '../state/doctorList.state';
import { loadDoctors, loadDoctorsSuccess, loadDoctorsFail,addExistingDoctorToList } from '../actions/doctorList.actions';
const initialState: DoctorListState = {
doctors:null,
error: null,
isLoading: false
};
const doctorListReducer = createReducer(initialState,
on(loadDoctors, (state) => {
return { ...state, isLoading: true };
}),
on(loadDoctorsSuccess, (state, { doctors }) => {
return { ...state, doctors: doctors, isLoading: false };
}),
on(loadDoctorsFail, ((state, { errorMessage }) => {
return { ...state, errorMes: errorMessage, isLoading: false };
})),
on(addExistingDoctorToList, (state, { doctor }) => {
return { ...state, doctors:[...state.doctors,doctor], isLoading: false };
}),
);
export function reducer(state: DoctorListState | undefined, action: Action) {
return doctorListReducer(state, action);
}我也将我的initialState doctors属性更改为空数组.then此错误没有出现,但我的医生列表也没有显示在组件中
这就是我在组件初始化时如何让医生访问我的doctors.list.component.ts的方法。
this.store.pipe(select(selectDoctorsList)).pipe(skip(1)).subscribe(payload => {
this.doctorList = payload;
})添加新的docotr事件单击函数
addNewDoctor(){
this.store.dispatch(addExistingDoctorToList({doctor:selectedDoctor}))
}我不明白我在这里做错了什么。
发布于 2020-02-18 16:33:33
查看操作定义
export const loadDoctorsSuccess = createAction('[Doctors] Doctors List Load Success', props<{ doctors: Doctors[] }>())
export const addExistingDoctorToList = createAction('[Doctors] Doctors List Load Success', props<{ doctor: Doctors }>())这两个操作具有用作action.type的相同字符串。它们应该是唯一的,因此ngrx会将它们视为不同的操作。
https://stackoverflow.com/questions/60275806
复制相似问题