Skip to content

Commit 208e89d

Browse files
committed
m10: router store feature
1 parent 7a3200c commit 208e89d

6 files changed

+95
-17
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
/tmp
66
/out-tsc
77

8+
# line count
9+
/out
10+
811
# dependencies
912
/node_modules
1013

src/app/app.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NgModule } from '@angular/core';
33

44
import { AppComponent } from './app.component';
55
import { HomeModule } from './home/home.module';
6-
import { RoutingModule } from './routing.module';
6+
import { RoutingModule } from './router/routing.module';
77
import { CartModule } from './cart/cart.module';
88
import {
99
MatToolbarModule,
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { RouterStateSerializer } from '@ngrx/router-store';
2+
import { Params, RouterStateSnapshot } from '@angular/router';
3+
4+
export interface RouterStateUrl {
5+
url: string;
6+
params: Params;
7+
}
8+
9+
export class CustomRouterSerializer
10+
implements RouterStateSerializer<RouterStateUrl> {
11+
serialize(routerState: RouterStateSnapshot): RouterStateUrl {
12+
// Select all the params available from current state.
13+
const params = routerState.root.children.map(c => c.params).reduce(
14+
(acc, p) => ({
15+
...acc,
16+
...p,
17+
}),
18+
{}
19+
);
20+
return {
21+
url: routerState.url,
22+
// Collect params from all levels
23+
params,
24+
};
25+
}
26+
}

src/app/router/routing.module.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { NgModule } from '@angular/core';
2+
import { RouterModule, Routes } from '@angular/router';
3+
4+
import { CartDetailsComponent } from '../cart-details/cart-details.component';
5+
import { HomeComponent } from '../home/home.component';
6+
import { ProductDetailsComponent } from '../product-details/product-details.component';
7+
8+
import {
9+
StoreRouterConnectingModule,
10+
routerReducer,
11+
RouterStateSerializer,
12+
RouterReducerState,
13+
} from '@ngrx/router-store';
14+
import { StoreModule } from '@ngrx/store';
15+
import {
16+
CustomRouterSerializer,
17+
RouterStateUrl,
18+
} from './custom-router-serializer';
19+
import { ROUTER_FEATURE_KEY } from './selectors';
20+
21+
const routes: Routes = [
22+
{ path: 'details/:productId', component: ProductDetailsComponent },
23+
{ path: 'cart', component: CartDetailsComponent },
24+
{ path: 'home', component: HomeComponent },
25+
{ path: '', redirectTo: '/home', pathMatch: 'full' },
26+
];
27+
28+
const initialState: RouterReducerState<RouterStateUrl> = {
29+
state: { url: '/', params: {} },
30+
navigationId: -1,
31+
};
32+
33+
@NgModule({
34+
imports: [
35+
RouterModule.forRoot(routes),
36+
StoreModule.forFeature(ROUTER_FEATURE_KEY, routerReducer, { initialState }),
37+
StoreRouterConnectingModule.forRoot({
38+
stateKey: ROUTER_FEATURE_KEY,
39+
}),
40+
],
41+
exports: [RouterModule],
42+
providers: [
43+
{ provide: RouterStateSerializer, useClass: CustomRouterSerializer },
44+
],
45+
})
46+
export class RoutingModule {}

src/app/router/selectors.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createFeatureSelector, createSelector } from '@ngrx/store';
2+
import { RouterReducerState } from '@ngrx/router-store';
3+
import { RouterStateUrl } from './custom-router-serializer';
4+
5+
export const ROUTER_FEATURE_KEY = 'Router feature';
6+
7+
export const routerFeatureState = createFeatureSelector<
8+
RouterReducerState<RouterStateUrl>
9+
>(ROUTER_FEATURE_KEY);
10+
11+
export const routerState = createSelector(
12+
routerFeatureState,
13+
featureState => featureState.state
14+
);
15+
16+
export const getRouterParams = createSelector(
17+
routerState,
18+
state => state.params
19+
);

src/app/routing.module.ts

-16
This file was deleted.

0 commit comments

Comments
 (0)