1
1
import { NgModule } from '@angular/core' ;
2
- import { Route , RouterModule } from '@angular/router' ;
2
+ import {
3
+ ActivatedRouteSnapshot ,
4
+ BaseRouteReuseStrategy ,
5
+ DetachedRouteHandle ,
6
+ Route ,
7
+ RouteReuseStrategy ,
8
+ RouterModule ,
9
+ } from '@angular/router' ;
3
10
import { PlotsComponent } from './components/plots/plots.component' ;
4
11
import { TutorialComponent } from './components/tutorial/tutorial.component' ;
5
12
@@ -14,3 +21,39 @@ const routes: Route[] = [
14
21
exports : [ RouterModule ] ,
15
22
} )
16
23
export class AppRoutingModule { }
24
+
25
+ export class AppRoutesReuseStrategy implements RouteReuseStrategy {
26
+ private cache : { [ key : string ] : DetachedRouteHandle } = { } ;
27
+
28
+ shouldDetach ( route : ActivatedRouteSnapshot ) : boolean {
29
+ return true ;
30
+ }
31
+
32
+ store ( route : ActivatedRouteSnapshot , handler : DetachedRouteHandle ) : void {
33
+ if ( handler ) {
34
+ this . cache [ this . getUrl ( route ) ] = handler ;
35
+ }
36
+ }
37
+
38
+ shouldAttach ( route : ActivatedRouteSnapshot ) : boolean {
39
+ return ! ! this . cache [ this . getUrl ( route ) ] ;
40
+ }
41
+
42
+ shouldReuseRoute (
43
+ future : ActivatedRouteSnapshot ,
44
+ current : ActivatedRouteSnapshot
45
+ ) : boolean {
46
+ return future . routeConfig === current . routeConfig ;
47
+ }
48
+
49
+ retrieve ( route : ActivatedRouteSnapshot ) {
50
+ if ( ! route . routeConfig || route . routeConfig . loadChildren ) {
51
+ return null ;
52
+ }
53
+ return this . cache [ this . getUrl ( route ) ] ;
54
+ }
55
+
56
+ private getUrl ( route : ActivatedRouteSnapshot ) : string {
57
+ return route . routeConfig ?. path ?? '' ;
58
+ }
59
+ }
0 commit comments