seimin 1 månad sedan
förälder
incheckning
6f3b377915

+ 3 - 3
proxy.conf.json

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "/service": {
3
-		"target": "http://192.168.8.108",
3
+		"target": "http://192.168.3.108",
4 4
     "logLevel": "debug",
5 5
     "changeOrigin": true,
6 6
     "pathRewrite": {
@@ -8,7 +8,7 @@
8 8
     }
9 9
   },
10 10
   "/file": {
11
-    "target": "http://192.168.8.108",
11
+    "target": "http://192.168.3.108",
12 12
     "logLevel": "debug",
13 13
     "changeOrigin": true,
14 14
     "pathRewrite": {
@@ -16,7 +16,7 @@
16 16
     }
17 17
   },
18 18
   "/record": {
19
-    "target": "http://192.168.8.108",
19
+    "target": "http://192.168.3.108",
20 20
     "logLevel": "debug",
21 21
     "changeOrigin": true,
22 22
     "pathRewrite": {

+ 66 - 0
src/app/SimpleReuseStrategy.ts

@@ -0,0 +1,66 @@
1
+import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle, UrlSegment } from '@angular/router';
2
+
3
+export class SimpleReuseStrategy implements RouteReuseStrategy {
4
+  static cacheRouters = new Map<string, DetachedRouteHandle>();
5
+
6
+  public static deleteRouteCache(url): void {
7
+    if (SimpleReuseStrategy.cacheRouters.has(url)) {
8
+      const handle: any = SimpleReuseStrategy.cacheRouters.get(url);
9
+      try {
10
+        handle.componentRef.destory();
11
+      } catch (e) { }
12
+      SimpleReuseStrategy.cacheRouters.delete(url);
13
+    }
14
+  }
15
+
16
+  public static deleteAllRouteCache(): void {
17
+    SimpleReuseStrategy.cacheRouters.forEach((handle: any, key) => {
18
+      SimpleReuseStrategy.deleteRouteCache(key);
19
+    });
20
+  }
21
+
22
+  // one 进入路由触发,是否同一路由时复用路由
23
+  shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
24
+    return future.routeConfig === curr.routeConfig &&
25
+      JSON.stringify(future.params) === JSON.stringify(curr.params);
26
+  }
27
+
28
+  // 获取存储路由
29
+  retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
30
+    const url = this.getFullRouteURL(route);
31
+    if (route.data.reuse && SimpleReuseStrategy.cacheRouters.has(url)) {
32
+      return SimpleReuseStrategy.cacheRouters.get(url);
33
+    } else {
34
+      return null;
35
+    }
36
+  }
37
+
38
+  // 是否允许复用路由
39
+  shouldDetach(route: ActivatedRouteSnapshot): boolean {
40
+    return Boolean(route.data.reuse);
41
+  }
42
+  // 当路由离开时会触发,存储路由
43
+  store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
44
+    const url = this.getFullRouteURL(route);
45
+    SimpleReuseStrategy.cacheRouters.set(url, handle);
46
+  }
47
+  //  是否允许还原路由
48
+  shouldAttach(route: ActivatedRouteSnapshot): boolean {
49
+    const url = this.getFullRouteURL(route);
50
+    return Boolean(route.data.reuse) && SimpleReuseStrategy.cacheRouters.has(url);
51
+  }
52
+
53
+  // 获取当前路由url
54
+  private getFullRouteURL(route: ActivatedRouteSnapshot): string {
55
+    const { pathFromRoot } = route;
56
+    let fullRouteUrlPath: string[] = [];
57
+    pathFromRoot.forEach((item: ActivatedRouteSnapshot) => {
58
+      fullRouteUrlPath = fullRouteUrlPath.concat(this.getRouteUrlPath(item));
59
+    });
60
+    return `/${fullRouteUrlPath.join('/')}`;
61
+
62
+  }
63
+  private getRouteUrlPath(route: ActivatedRouteSnapshot) {
64
+    return route.url.map(urlSegment => urlSegment.path);
65
+  }
66
+}

+ 2 - 2
src/app/app.module.ts

@@ -1,4 +1,3 @@
1
-import { CustomReuseStrategy } from './custom-route-reuse-strategy';
2 1
 import { BrowserModule } from '@angular/platform-browser';
3 2
 import { NgModule } from '@angular/core';
4 3
 import { CommonModule } from '@angular/common';
@@ -12,6 +11,7 @@ import { ShareModule } from './share/share.module';
12 11
 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
13 12
 import { NZ_I18N, zh_CN, NzConfig, NZ_CONFIG } from 'ng-zorro-antd';
14 13
 import { RouteReuseStrategy } from '@angular/router';
14
+import { SimpleReuseStrategy } from './SimpleReuseStrategy';
15 15
 const ngZorroConfig: NzConfig = {
16 16
   // 注意组件名称没有 nz 前缀
17 17
   notification: { nzTop: 100 }
@@ -34,7 +34,7 @@ registerLocaleData(zh);
34 34
     { provide: NZ_CONFIG, useValue: ngZorroConfig },
35 35
     { provide: LocationStrategy, useClass: HashLocationStrategy },
36 36
     { provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true },
37
-    { provide: RouteReuseStrategy, useClass: CustomReuseStrategy },
37
+    { provide: RouteReuseStrategy, useClass: SimpleReuseStrategy },
38 38
   ],
39 39
   bootstrap: [AppComponent]
40 40
 })

+ 0 - 48
src/app/custom-route-reuse-strategy.ts

@@ -1,48 +0,0 @@
1
-import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router';
2
-
3
-export class CustomReuseStrategy implements RouteReuseStrategy {
4
-  private storedHandles = new Map<string, DetachedRouteHandle>();
5
-
6
-  shouldDetach(route: ActivatedRouteSnapshot): boolean {
7
-    return route.data.reuse || false;
8
-  }
9
-
10
-  store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
11
-    if (route.data.reuse) {
12
-      const key = this.getRouteKey(route);
13
-      this.storedHandles.set(key, handle);
14
-    }
15
-  }
16
-
17
-  shouldAttach(route: ActivatedRouteSnapshot): boolean {
18
-    const key = this.getRouteKey(route);
19
-    // console.log('shouldAttach:', this.storedHandles.has(key))
20
-    return this.storedHandles.has(key);
21
-  }
22
-
23
-  retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null {
24
-    const key = this.getRouteKey(route);
25
-    return this.storedHandles.get(key) || null;
26
-  }
27
-
28
-  shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
29
-    // 精确匹配路由复用
30
-    return future.routeConfig === curr.routeConfig &&
31
-           JSON.stringify(future.params) === JSON.stringify(curr.params);
32
-  }
33
-
34
-  private getRouteKey(route: ActivatedRouteSnapshot): string {
35
-    let str = route.pathFromRoot
36
-    .filter(v => v.routeConfig)
37
-    .map(v => v.routeConfig!.path + JSON.stringify(v.params))
38
-    .join('/');
39
-    return str.endsWith('/{}') ? str.slice(0, -3) : str;
40
-  }
41
-
42
-  public deleteRouteSnapshot(url: string): void {
43
-    const key = url.replace(/\//g, '_');
44
-    if (this.storedHandles.has(key)) {
45
-      this.storedHandles.delete(key);
46
-    }
47
-  }
48
-}

+ 13 - 2
src/app/views/new-statistics/maintenance-statistics/maintenance-statistics.component.ts

@@ -1,6 +1,8 @@
1 1
 import { ActivatedRoute, Router } from '@angular/router';
2 2
 import { Component, OnInit, OnDestroy } from "@angular/core";
3 3
 import { ToolService } from 'src/app/services/tool.service';
4
+import { SimpleReuseStrategy } from '../../../SimpleReuseStrategy';
5
+import { TabService } from '../services/tab.service';
4 6
 @Component({
5 7
   selector: "app-maintenance-statistics",
6 8
   templateUrl: "./maintenance-statistics.component.html",
@@ -11,6 +13,7 @@ export class MaintenanceStatisticsComponent implements OnInit, OnDestroy {
11 13
     public tool: ToolService,
12 14
     public route: ActivatedRoute,
13 15
     public router: Router,
16
+    public tabService: TabService,
14 17
   ) {}
15 18
 
16 19
   menuList: any[] = [];
@@ -24,6 +27,8 @@ export class MaintenanceStatisticsComponent implements OnInit, OnDestroy {
24 27
 
25 28
   ngOnDestroy(){
26 29
     this.sessionRemove();
30
+    this.tabService.tabs = [];
31
+    SimpleReuseStrategy.deleteAllRouteCache();
27 32
   }
28 33
 
29 34
   getMenuList(){
@@ -36,6 +41,8 @@ export class MaintenanceStatisticsComponent implements OnInit, OnDestroy {
36 41
   clickMenu(data){
37 42
     this.activeMenuId = data.id;
38 43
     this.secondMenuList = this.menuList.find(v => v.id == this.activeMenuId).childrens || [];
44
+    this.tabService.tabs = [];
45
+    SimpleReuseStrategy.deleteAllRouteCache();
39 46
     this.secondMenuList.length && this.clickSecondMenu(this.secondMenuList[0]);
40 47
   }
41 48
 
@@ -43,8 +50,12 @@ export class MaintenanceStatisticsComponent implements OnInit, OnDestroy {
43 50
   activeSecondMenuLink:string;
44 51
   clickSecondMenu(data){
45 52
     this.activeSecondMenuLink = data.link;
46
-    // this.router.navigateByUrl(`newStatistics/${this.route.parent.snapshot.routeConfig.path}/${this.activeSecondMenuLink}`).finally();
47
-    this.router.navigate([`newStatistics/${this.route.parent.snapshot.routeConfig.path}/${this.activeSecondMenuLink}`], { replaceUrl: true });
53
+    let hasCache = SimpleReuseStrategy.cacheRouters.has(`/newStatistics/${this.route.parent.snapshot.routeConfig.path}/${this.activeSecondMenuLink}`);
54
+    console.log('tabs', this.tabService.tabs);
55
+    console.log('cacheRouters', SimpleReuseStrategy.cacheRouters);
56
+    console.log('cacheRouters', hasCache);
57
+    !hasCache && SimpleReuseStrategy.deleteRouteCache(`/newStatistics/${this.route.parent.snapshot.routeConfig.path}/${this.activeSecondMenuLink}`);
58
+    this.router.navigate([`/newStatistics/${this.route.parent.snapshot.routeConfig.path}/${this.activeSecondMenuLink}`], { replaceUrl: true });
48 59
   }
49 60
 
50 61
   // 回显二级菜单

+ 11 - 1
src/app/views/new-statistics/services/tab.service.ts

@@ -1,6 +1,7 @@
1 1
 import { Injectable } from '@angular/core';
2 2
 import { Router, NavigationEnd, ActivatedRouteSnapshot } from '@angular/router';
3 3
 import { filter } from 'rxjs/operators';
4
+import { SimpleReuseStrategy } from 'src/app/SimpleReuseStrategy';
4 5
 
5 6
 export interface Tab {
6 7
   title: string;
@@ -58,6 +59,9 @@ export class TabService {
58 59
   }
59 60
 
60 61
   closeTab(path: string): any {
62
+    console.log('path:', path)
63
+    SimpleReuseStrategy.deleteRouteCache(path);
64
+
61 65
     const index = this.tabs.findIndex(t => t.path === path);
62 66
     if (index === -1) return;
63 67
 
@@ -89,7 +93,7 @@ export class TabService {
89 93
         replaceUrl: true
90 94
       });
91 95
     }
92
-    return newActiveTab!.path;
96
+    return newActiveTab.path;
93 97
   }
94 98
 
95 99
   private getTitleFromRoute(route: ActivatedRouteSnapshot): string {
@@ -108,6 +112,12 @@ export class TabService {
108 112
     keepTab.active = true;
109 113
     this.currentTab = keepTab;
110 114
 
115
+    this.tabs.forEach(v => {
116
+      if (v.path !== keepTab.path) {
117
+        SimpleReuseStrategy.deleteRouteCache(v.path);
118
+      }
119
+    })
120
+
111 121
     // 触发路由跳转
112 122
     this.router.navigate([keepTab.path], {
113 123
       replaceUrl: true