Skip to content

Commit 81bc0d9

Browse files
feat(router-store): add new selectRouteDataParam selector (#3673) (#3686)
Closes #3673
1 parent cae65bd commit 81bc0d9

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

modules/router-store/spec/router_selectors.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ describe('Router State Selectors', () => {
227227
);
228228
});
229229

230+
it('should create a selector for selecting a specific route data param', () => {
231+
const result = selectors.selectRouteDataParam('testData')(state);
232+
233+
expect(result).toEqual(
234+
state.router.state.root.firstChild.firstChild.data.testData
235+
);
236+
});
237+
230238
it('should create a selector for selecting the url', () => {
231239
const result = selectors.selectUrl(state);
232240

modules/router-store/src/models.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export interface RouterStateSelectors<V> {
99
selectRouteParams: MemoizedSelector<V, Params>;
1010
selectRouteParam: (param: string) => MemoizedSelector<V, string | undefined>;
1111
selectRouteData: MemoizedSelector<V, Data>;
12+
selectRouteDataParam: (
13+
param: string
14+
) => MemoizedSelector<V, string | undefined>;
1215
selectUrl: MemoizedSelector<V, string>;
1316
selectTitle: MemoizedSelector<V, string | undefined>;
1417
}

modules/router-store/src/router_selectors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export function getSelectors<V extends Record<string, any>>(
5454
selectCurrentRoute,
5555
(route) => route && route.data
5656
);
57+
const selectRouteDataParam = (param: string) =>
58+
createSelector(selectRouteData, (data) => data && data[param]);
5759
const selectUrl = createSelector(
5860
selectRouterState,
5961
(routerState) => routerState && routerState.url
@@ -75,6 +77,7 @@ export function getSelectors<V extends Record<string, any>>(
7577
selectRouteParams,
7678
selectRouteParam,
7779
selectRouteData,
80+
selectRouteDataParam,
7881
selectUrl,
7982
selectTitle,
8083
};

projects/ngrx.io/content/examples/router-store-selectors/src/app/router.selectors.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { getSelectors, RouterReducerState } from '@ngrx/router-store';
66
// export const selectRouter = createFeatureSelector<RouterReducerState>('yourFeatureName');
77

88
export const {
9-
selectCurrentRoute, // select the current route
10-
selectFragment, // select the current route fragment
11-
selectQueryParams, // select the current route query params
12-
selectQueryParam, // factory function to select a query param
13-
selectRouteParams, // select the current route params
14-
selectRouteParam, // factory function to select a route param
15-
selectRouteData, // select the current route data
16-
selectUrl, // select the current url
17-
selectTitle, // Select the title if available
9+
selectCurrentRoute, // select the current route
10+
selectFragment, // select the current route fragment
11+
selectQueryParams, // select the current route query params
12+
selectQueryParam, // factory function to select a query param
13+
selectRouteParams, // select the current route params
14+
selectRouteParam, // factory function to select a route param
15+
selectRouteData, // select the current route data
16+
selectRouteDataParam, // factory function to select a route data param
17+
selectUrl, // select the current url
18+
selectTitle, // select the title if available
1819
} = getSelectors();
1920
// #enddocregion routerSelectors

0 commit comments

Comments
 (0)