|
| 1 | +import { ModuleWithProviders } from '@angular/core'; |
| 2 | +import { Routes, RouterModule, RouteReuseStrategy, DetachedRouteHandle, ActivatedRouteSnapshot } from '@angular/router'; |
| 3 | +import { AppComponent } from './app.component'; |
| 4 | + |
| 5 | +import { DemoComponent } from './demo/demo/demo.component'; |
| 6 | +import { Demo1Component } from './demo/demo1/demo1.component'; |
| 7 | +import { Demo2Component } from './demo/demo2/demo2.component'; |
| 8 | +import { Demo3Component } from './demo/demo3/demo3.component'; |
| 9 | + |
| 10 | +const appRoutes: Routes = [ |
| 11 | + { |
| 12 | + path: '', |
| 13 | + redirectTo: 'demo', |
| 14 | + pathMatch: 'full' |
| 15 | + }, |
| 16 | + { path: 'demo1', component: Demo1Component }, |
| 17 | + { path: 'demo2', component: Demo2Component }, |
| 18 | + { path: 'demo3', component: Demo3Component } |
| 19 | +]; |
| 20 | + |
| 21 | +export const appRoutingProviders: any[] = []; |
| 22 | + |
| 23 | +export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); |
| 24 | + |
| 25 | +export class CustomReuseStrategy implements RouteReuseStrategy { |
| 26 | + |
| 27 | + handlers: { [key: string]: DetachedRouteHandle } = {}; |
| 28 | + |
| 29 | + shouldDetach(route: ActivatedRouteSnapshot): boolean { |
| 30 | + console.log('CustomReuseStrategy:shouldDetach', route); |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void { |
| 35 | + console.log('CustomReuseStrategy:store', route, handle); |
| 36 | + this.handlers[route.routeConfig.path] = handle; |
| 37 | + } |
| 38 | + |
| 39 | + shouldAttach(route: ActivatedRouteSnapshot): boolean { |
| 40 | + console.log('CustomReuseStrategy:shouldAttach', route); |
| 41 | + return !!route.routeConfig && !!this.handlers[route.routeConfig.path]; |
| 42 | + } |
| 43 | + |
| 44 | + retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { |
| 45 | + console.log('CustomReuseStrategy:retrieve', route); |
| 46 | + return this.handlers[route.routeConfig.path];//从暂存处取回 |
| 47 | + } |
| 48 | + |
| 49 | + shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { |
| 50 | + //在此处可以取得跳转前和跳转后的路由路径 |
| 51 | + console.log('CustomReuseStrategy:shouldReuseRoute', future, curr); |
| 52 | + return future.routeConfig === curr.routeConfig; |
| 53 | + } |
| 54 | +} |
0 commit comments