我正在尝试通过ID从实体状态中选择一个对象。下面是我的减速器。
export interface MessageState extends EntityState<Message> {
// additional entities state properties
loaded: boolean;
loading: boolean;
}
export const adapter: EntityAdapter<Message> = createEntityAdapter<Message>({
selectId: (msg: Message) => msg.messageId,
});
export const initialState: MessageState = adapter.getInitialState({
// additional entity state properties
loaded: false,
loading: false,
});
export function reducer(state = initialState, action: MessageActionsUnion): MessageState {
switch (action.type) {
case MessageActionTypes.UPSERT_Message: {
return { ...state, loading: true, loaded: false };
}
case MessageActionTypes.UPSERT_Message_SUCCESS: {
return adapter.upsertOne(action.payload.Message,
{
...state, loaded: true, loading: false,
});
}
default: {
return state;
}
}
}
下面是我的index.ts文件
export interface State extends fromRoot.AppState {
queueModule: QueueState;
}
export interface QueueState {
msgHeaders: fromMsgHeaders.MessageHeaderState
}
export const reducers: ActionReducerMap<QueueState> = {
msgHeaders: fromMsgHeaders.reducer
};
export const getQueueState$ = createFeatureSelector<QueueState>('queueModule');
我试图在传递ID时形成选择器,该ID将从实体返回值。
export const selectMessages = createSelector(
fromFeatures.getQueueState$,
(state: fromFeatures.QueueState) => state.msgs
);
export const {
selectAll: selectAllMessages,
selectEntities: selectMessagesEntities,
selectIds: selectMessagesIds,
selectTotal: selectMessagesTotal
} = adapter.getSelectors(selectMessages);
我看了很多资源,但是不清楚如何通过传递ID选择对象。
答案 0 :(得分:1)
sat p = for c from item: -- do { c <- item
if p c -- ; if p c
then return c -- then return c
else failParse -- else failParse }
没有提供@ngrx/entity
选择器。
在大多数情况下,这是您不需要做的事情。
话虽这么说,但有多种方法可以创建一个选择器来做到这一点,有关更多信息,请参见(NgRx:参数化选择器)[https://blog.angularindepth.com/ngrx-parameterized-selector-e3f610529f8]。
例如:
getOneById
答案 1 :(得分:0)
@ngrx/entity
在发布了dictionary
的v6.1.0(ngrx entity changelog)版本之后确实提供了getEntityValueById。
因此,我可以按以下方式通过其ID提取实体的单个值。
// in the selector
import { Dictionary} from '@ngrx/entity;
export const getMessageById = () => {
return createSelector(
selectMessagesEntities,
(entities: Dictionary<Message>, props: { messageId: number }) => {
return entities[props.messageId];
},
);
};
//to call the selector
this.msg$ = this.store.pipe(select(
fromStore.getMessageById(), { messageId: 10 }
)).pipe(
map(
(message: Message) => {
return this.msg = message;
},
)
);
此方法也有很好的参考文献 here。