所以我有一个自定义实体,我将在UI中添加它:
meeting.ts:
export interface Meeting {
summary: MeetingSummary;
agenda: any;
audience?: Audience[];
venue?: {
lat?: number;
long?: number;
};
invitationKey?: string;
key: string;
activeProposalId?: string;
createdAt?: string;
updatedAt?: string;
createdBy?: string;
updatedBy?: string;
owner?: string;
}
我使用新的创建者语法来搭建@ ngrx / entity:
meeting.actions.ts:
export const beginLoadMeetings = createAction(
'[ Meeting ] Begin Load Meetings',
props<{ uid: string }>()
);
export const loadMeetings = createAction(
'[Meeting/API] Load Meetings',
props<{ meetings: Meeting[] }>()
);
export const beginAddMeeting = createAction(
'[ Meeting ] Begin Add New Meeting',
props<{ meeting: Meeting }>()
);
export const addMeeting = createAction(
'[ Meeting ] Add Meeting to Store',
props<{ meeting: Meeting }>()
);
export const upsertMeeting = createAction(
'[Meeting/API] Upsert Meeting',
props<{ meeting: Meeting }>()
);
export const addMeetings = createAction(
'[Meeting/API] Add Meetings',
props<{ meetings: Meeting[] }>()
);
// ...
以及相应的减速器
meeting.reducer.ts
export interface MeetingState extends EntityState<Meeting> {
loading: boolean;
}
export interface State extends fromRoot.State {
meetings: MeetingState;
}
export const adapter: EntityAdapter<Meeting> = createEntityAdapter<Meeting>(
{
selectId: (meeting: Meeting) => (meeting.key)
}
);
export const initialState: MeetingState = adapter.getInitialState({
loading: false
});
const meetingReducer = createReducer(
initialState,
on(MeetingActions.beginLoadMeetings,
(state) => ({...state, loading: true})
),
on(MeetingActions.beginAddMeeting,
(state) => state
),
on(MeetingActions.addMeeting,
(state, action) => adapter.addOne(action.meeting, state)
),
on(MeetingActions.upsertMeeting,
(state, action) => adapter.upsertOne(action.meeting, state)
),
//...
);
我将分派一个beginAddMeeting
动作,该动作触发一个存储到后端的效果。这种情况可以正确发生,并且效果还可以正确分配有效负载的addMeeting
操作,我可以在DevTools中进行验证。但是,状态不会更改,并且实体也不会添加到商店中,因此我所有的容器都无法记录该更改。
奇怪的是,当我调度beginLoadMeetings
时,它会触发类似的效果,只从后端获取所有实体,因此商店得到了正确的更新。简而言之,addMany
有效,addOne
和upsertOne
对状态不起作用。当我在没有适配器的情况下进行更新时,reducer甚至没有反应,这使我怀疑我的配置有问题。我们花了半个星期来解决这个问题,但没有成功,因此希望在这里能对您有所帮助。谢谢!