我有一个状态,我想从 ngrx/data 实体创建选择器。
import {
Action,
ActionReducer,
ActionReducerMap,
createFeatureSelector,
createSelector,
MetaReducer
} from '@ngrx/store';
import {environment} from '../../environments/environment';
import * as fromRouter from '@ngrx/router-store';
import * as fromDrawer from './drawer';
import {InjectionToken} from '@angular/core';
import {NavigationItem} from '../models/navigation-item';
export interface State {
router: fromRouter.RouterReducerState<any>;
drawerNavigationItems: fromDrawer.State;
}
export const ROOT_REDUCERS = new InjectionToken<ActionReducerMap<State, Action>>('Root reducers token', {factory: () => ({
router: fromRouter.routerReducer,
drawerNavigationItems: fromDrawer.reducer,
}),
});
export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
export const selectRouter = createFeatureSelector<
State,
fromRouter.RouterReducerState<any>
>('router');
const {
selectQueryParams, // select the current route query params
selectQueryParam, // factory function to select a query param
selectRouteParams, // select the current route params
selectRouteParam, // factory function to select a route param
selectRouteData, // select the current route data
selectUrl, // select the current url
} = fromRouter.getSelectors(selectRouter);
export const selectRouteId = selectRouteParam('id');
export const selectStatus = selectQueryParam('status');
// Drawer
export const selectDrawerNavigationItems = (state: State) => state.drawerNavigationItems.items as NavigationItem[];
如何使用预定义的选择器或使用来自 ngrx/data 的实体或服务编写自己的选择器?
例如,我想创建一个选择器来选择所有“社区”实体,然后在第 2 步中选择 1 by selectRouteId
。如果您想象一个 route /communities/:id
,selectRouteId
返回 Id,现在我想要来自的数据CommunityService
并使用在步骤 1 中创建或以某种方式导入和使用的选择器并返回一个结果 1Community
和selectRouteId
id,所以我以后可以做就像是this.store.dispatch(selectCommunityByCurrentRouteId);
这个问题特定于@ngrx/data。