尝试在NgRx中使用参数化查询,但结果为空。
这是下面的代码的堆叠版本:https://stackblitz.com/edit/ngrx-parameterized-query
还原代码
export const userAdapter = createEntityAdapter<UserModel>({
selectId: x => x.UserId
});
export interface UserlistState extends EntityState<UserModel>{
};
export const initialState: UserlistState = userAdapter.getInitialState({
});
export function UserlistReducers(
state: UserlistState = initialState,
action: actions.UserlistActions
){
switch(action.type){
case actions.UserlistActionTypes.UPSERT_USERS:
return userAdapter.upsertMany(action.users,state);
default:
return state;
}
}
export const getUserlistState = createFeatureSelector<UserlistState>('userlist');
export const {
selectIds,
selectEntities,
selectAll,
selectTotal
} = userAdapter.getSelectors(getUserlistState);
export const getUsersByOrganizationId = createSelector(selectAll,
(users: Array<UserModel>, organization_id: number)=>{
return users.filter(x=> x.OrganizationId == organization_id)
}
)
用法
watchlist = ():Observable<UserModel[]> =>{
return this.userlistStore.select(reducers.selectAll);
}
watchlistByOrganizationId = (organization_id: number): Observable<UserModel[]> =>{
return this.userlistStore.select(reducers.getUsersByOrganizationId(organization_id));
}
答案 0 :(得分:0)
您的选择器错误。
这应该是这样; organization_id
是选择器函数的参数,因此它在函数范围内可用,因此无需在箭头函数参数列表中添加它。 selectAll
将返回用户并将其作为参数传递给arrow函数。以下应该可以正常工作!
export const getUsersByOrganizationId = (organization_id: number) => createSelector(selectAll,
(users: Array<UserModel>) => {
return users.filter(x => x.OrganizationId == organization_id)
}
);