如何使用选择器仅通过特定键获取数据?
也就是说,我有4个菜单,每个菜单都有一个特定的log(f(x)) = a+b*log(P)
a = 12, b = -0.8
fit f(x) 'data.txt' u 1:2 w p via a,b
plot f(x) 'data.txt' u 1:2
,用户单击即可从服务器获取特定菜单的列表。该列表的每个元素都有一个catalogCode
。也就是说,此菜单所有元素的id = code + catalogCode
将相同。
那么如何从商店中获取特定catalogCode
的商品清单?
我正在尝试这样做:catalogCode
selectByCatalogCode
但这在组件中使用时不起作用,始终显示未定义的值
export const selectCatalogItemState: MemoizedSelector<object, State> = createFeatureSelector('CatalogItem');
export const selectCatalogItemAll: MemoizedSelector<object, CatalogItem[]> = createSelector(selectCatalogItemState, selectAll);
export const selectByCatalogCode = () => createSelector(selectCatalogItemAll, (entities: CatalogItem[], props: { catalogCode: string }) => entities[props.catalogCode]);
catalog-item.ts
getCatalogItems(catalogCode: string) {
this.catalogItemStore.dispatch(new CatalogItemStoreAction.LoadByCatalog({catalogCode: catalogCode}));
this.catalogItemStore.select(CatalogItemStoreSelector.selectByCatalogCode(), {catalogCode: catalogCode}).subscribe(
a => console.log(a)
);
}
我只提供了部分代码。如有必要,我可以提供整个商店的代码。
effect.ts
export class CatalogItem {
constructor(public code: string,
public catalogCode: string,
public title: string,
public data: object) {
}
}
catalogs-list.component.ts
@Effect()
getCatalogEffect$: Observable<Action> = this.action$
.pipe(
ofType<featureAction.GetByCatalog>(featureAction.ActionTypes.GET_BY_CATALOG),
switchMap(action => this.store.select(CatalogItemStoreSelector.selectByCatalogCode(action.payload.catalogCode))
.pipe(
take(1),
filter(catalogItems => !catalogItems),
map(() => new featureAction.LoadByCatalog({catalogCode: action.payload.catalogCode})),
)
)
);
@Effect()
loadByCatalog$: Observable<Action> = this.action$
.pipe(
ofType<featureAction.LoadByCatalog>(featureAction.ActionTypes.LOAD_BY_CATALOG),
switchMap(action => this.catalogItemService.listByCatalog(action.payload.catalogCode)
.pipe(
map(catalogItems => new featureAction.LoadByCatalogSuccess({catalogItems: catalogItems})),
catchError(error => of(new featureAction.LoadByCatalogError({error: error}))),
)
)
);
答案 0 :(得分:0)
在选择器中,如果要获取与catalogCode匹配的对象,请使用过滤器。
TS:
this.catalogItemStore.select(CatalogItemStoreSelector.selectByCatalogCode(catalogCode)).subscribe(
a => console.log(a)
);
选择器:
export const selectByCatalogCode = (catalogCode: string) => createSelector(selectCatalogItemAll, (entities: CatalogItem[]) => entities.filter((item: CatalogItem) => item.catalogCode === catalogCode));