Skip to content

Commit a30a514

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(RouterStore): make the router store key selector generic
* feat(RouterStore): make the router store key selector generic * docs: add note that router stateKey can also be a selector Closes #1457
1 parent ab494b1 commit a30a514

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

modules/router-store/src/router_store_module.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ import {
2929
DefaultRouterStateSerializer,
3030
RouterStateSerializer,
3131
SerializedRouterStateSnapshot,
32+
BaseRouterStoreState,
3233
} from './serializer';
3334

34-
export type StateKeyOrSelector = string | Selector<any, RouterReducerState>;
35+
export type StateKeyOrSelector<
36+
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
37+
> = string | Selector<any, RouterReducerState<T>>;
3538

36-
export interface StoreRouterConfig {
37-
stateKey?: StateKeyOrSelector;
39+
export interface StoreRouterConfig<
40+
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
41+
> {
42+
stateKey?: StateKeyOrSelector<T>;
3843
serializer?: new (...args: any[]) => RouterStateSerializer;
3944
/**
4045
* By default, ROUTER_NAVIGATION is dispatched before guards and resolvers run.
@@ -136,8 +141,10 @@ enum RouterTrigger {
136141
],
137142
})
138143
export class StoreRouterConnectingModule {
139-
static forRoot(
140-
config: StoreRouterConfig = {}
144+
static forRoot<
145+
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
146+
>(
147+
config: StoreRouterConfig<T> = {}
141148
): ModuleWithProviders<StoreRouterConnectingModule> {
142149
return {
143150
ngModule: StoreRouterConnectingModule,

projects/ngrx.io/content/guide/router-store/configuration.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
```ts
44
interface StoreRouterConfig {
5-
stateKey?: string;
5+
stateKey?: string | Selector<any, RouterReducerState<T>>;
66
serializer?: new (...args: any[]) => RouterStateSerializer;
77
navigationActionTiming?: NavigationActionTiming;
88
}
99
```
1010

11-
- `stateKey`: The name of reducer key, defaults to `router`
11+
- `stateKey`: The name of reducer key, defaults to `router`. It's also possible to provide a selector function.
1212
- `serializer`: How a router snapshot is serialized. Defaults to `DefaultRouterStateSerializer`. See [Custom Router State Serializer](#custom-router-state-serializer) for more information.
1313
- `navigationActionTiming`: When the `ROUTER_NAVIGATION` is dispatched. Defaults to `NavigationActionTiming.PreActivation`. See [Navigation Action Timing](#navigation-action-timing) for more information.
1414

@@ -24,6 +24,7 @@ Your custom serializer should implement the abstract class `RouterStateSerialize
2424
You then provide the serializer through the config.
2525

2626
**In a custom serializer ts file**
27+
2728
```ts
2829
import { Params, RouterStateSnapshot } from '@angular/router';
2930
import { RouterStateSerializer } from '@ngrx/router-store';
@@ -56,13 +57,15 @@ export class CustomSerializer implements RouterStateSerializer<RouterStateUrl> {
5657
```
5758

5859
**In your root reducer**
60+
5961
```ts
6062
export const reducers: ActionReducerMap<State> = {
6163
router: routerReducer
6264
};
6365
```
6466

6567
**In your AppModule**
68+
6669
```ts
6770
@NgModule({
6871
imports: [

0 commit comments

Comments
 (0)