路由复用优化

This commit is contained in:
zhouyuejun
2018-12-19 13:40:46 +08:00
parent 1e735059f8
commit cdb09eefa2
15 changed files with 155 additions and 116 deletions
+12 -9
View File
@@ -12,28 +12,27 @@ interface ICachedRoute {
@Injectable()
export class AppReuseStrategy implements RouteReuseStrategy {
private routeCache = new Map<string, ICachedRoute>();
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return future.routeConfig === curr.routeConfig;
}
/** 是否需要复用 */
shouldDetach(route: ActivatedRouteSnapshot): boolean {
const data = this.getRouteData(route);
return data && data.reuse;
}
/** 存储路由快照 */
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
const url = this.getFullRouteUrl(route);
const data = this.getRouteData(route);
this.routeCache.set(url, { handle, data });
this.addRedirectsRecursively(route);
}
/** 是否允许还原 在缓存中有的都认为允许还原 */
shouldAttach(route: ActivatedRouteSnapshot): boolean {
const url = this.getFullRouteUrl(route);
if (url === 'login') {
this.routeCache = new Map<string, ICachedRoute>();
}
return this.routeCache.has(url);
}
/** 从缓存中获取快照 */
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
const url = this.getFullRouteUrl(route);
const data = this.getRouteData(route);
@@ -41,6 +40,10 @@ export class AppReuseStrategy implements RouteReuseStrategy {
? this.routeCache.get(url).handle
: null;
}
/** 同一路由时使用快照 */
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return future.routeConfig === curr.routeConfig;
}
private addRedirectsRecursively(route: ActivatedRouteSnapshot): void {
const config = route.routeConfig;
@@ -63,7 +66,7 @@ export class AppReuseStrategy implements RouteReuseStrategy {
}
private getFullRouteUrl(route: ActivatedRouteSnapshot): string {
return this.getFullRouteUrlPaths(route).filter(Boolean).join('/');
return this.getFullRouteUrlPaths(route).filter(Boolean).join('/').replace('/', '_');
}
private getFullRouteUrlPaths(route: ActivatedRouteSnapshot): string[] {