|
@@ -1,66 +1,58 @@
|
1
|
|
-import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle, UrlSegment } from '@angular/router';
|
|
1
|
+import {RouteReuseStrategy, DefaultUrlSerializer, ActivatedRouteSnapshot, DetachedRouteHandle} from '@angular/router';
|
2
|
2
|
|
|
3
|
+/**
|
|
4
|
+ * 路由复用策略
|
|
5
|
+ */
|
3
|
6
|
export class SimpleReuseStrategy implements RouteReuseStrategy {
|
4
|
|
- static cacheRouters = new Map<string, DetachedRouteHandle>();
|
5
|
7
|
|
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
|
|
- }
|
|
8
|
+ public static handlers: { [key: string]: DetachedRouteHandle } = {};
|
|
9
|
+ private static waitDelete: string;
|
|
10
|
+
|
|
11
|
+ /** 表示对所有路由允许复用 如果你有路由不想利用可以在这加一些业务逻辑判断 */
|
|
12
|
+ public shouldDetach(route: ActivatedRouteSnapshot): boolean {
|
|
13
|
+ return route.routeConfig && route.routeConfig.data && route.routeConfig.data.reuse === true;
|
14
|
14
|
}
|
15
|
15
|
|
16
|
|
- public static deleteAllRouteCache(): void {
|
17
|
|
- SimpleReuseStrategy.cacheRouters.forEach((handle: any, key) => {
|
18
|
|
- SimpleReuseStrategy.deleteRouteCache(key);
|
19
|
|
- });
|
|
16
|
+ /** 当路由离开时会触发。按path作为key存储路由快照&组件当前实例对象 */
|
|
17
|
+ public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
|
|
18
|
+ if (SimpleReuseStrategy.waitDelete && SimpleReuseStrategy.waitDelete === this.getRouteUrl(route)) {
|
|
19
|
+ // 如果待删除是当前路由则不存储快照
|
|
20
|
+ SimpleReuseStrategy.waitDelete = null;
|
|
21
|
+ return;
|
|
22
|
+ }
|
|
23
|
+ SimpleReuseStrategy.handlers[this.getRouteUrl(route)] = handle;
|
20
|
24
|
}
|
21
|
25
|
|
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
|
+ /** 若 path 在缓存中有的都认为允许还原路由 */
|
|
27
|
+ public shouldAttach(route: ActivatedRouteSnapshot): boolean {
|
|
28
|
+ return !!SimpleReuseStrategy.handlers[this.getRouteUrl(route)];
|
26
|
29
|
}
|
27
|
30
|
|
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 {
|
|
31
|
+ /** 从缓存中获取快照,若无则返回nul */
|
|
32
|
+ public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
|
|
33
|
+ if (!route.routeConfig) {
|
34
|
34
|
return null;
|
35
|
35
|
}
|
36
|
|
- }
|
37
|
36
|
|
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);
|
|
37
|
+ return SimpleReuseStrategy.handlers[this.getRouteUrl(route)];
|
51
|
38
|
}
|
52
|
39
|
|
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('/')}`;
|
|
40
|
+ /** 进入路由触发,判断是否同一路由 */
|
|
41
|
+ public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
|
|
42
|
+ return future.routeConfig === curr.routeConfig &&
|
|
43
|
+ JSON.stringify(future.params) === JSON.stringify(curr.params);
|
|
44
|
+ }
|
61
|
45
|
|
|
46
|
+ private getRouteUrl(route: ActivatedRouteSnapshot) {
|
|
47
|
+ return route['_routerState'].url.replace(/\//g, '_');
|
62
|
48
|
}
|
63
|
|
- private getRouteUrlPath(route: ActivatedRouteSnapshot) {
|
64
|
|
- return route.url.map(urlSegment => urlSegment.path);
|
|
49
|
+
|
|
50
|
+ public static deleteRouteSnapshot(url: string): void {
|
|
51
|
+ const key = url.replace(/\//g, '_');
|
|
52
|
+ if (SimpleReuseStrategy.handlers[key]) {
|
|
53
|
+ delete SimpleReuseStrategy.handlers[key];
|
|
54
|
+ } else {
|
|
55
|
+ SimpleReuseStrategy.waitDelete = key;
|
|
56
|
+ }
|
65
|
57
|
}
|
66
|
58
|
}
|