1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import { RouteReuseStrategy, DefaultUrlSerializer, ActivatedRouteSnapshot, DetachedRouteHandle, Route } from '@angular/router' ; import { ComponentRef } from '@angular/core' ; export class SimpleReuseStrategy implements RouteReuseStrategy { public static handlers: Map<Route, DetachedRouteHandle> = new Map(); private getRouteUrl(route?: ActivatedRouteSnapshot | any) { return route[ '_routerState' ].url.replace(/[\/, -]/g, '_' ) } public shouldDetach(_route: ActivatedRouteSnapshot): boolean { return true ; } /** 当路由离开时会触发。按path作为key存储路由快照&组件当前实例对象 */ public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void { if (!route.routeConfig) return ; SimpleReuseStrategy.handlers. set (route.routeConfig, handle); } /** 若 path 在缓存中有的都认为允许还原路由 */ public shouldAttach(route: ActivatedRouteSnapshot): boolean { return !!route.routeConfig && !!SimpleReuseStrategy.handlers. get (route.routeConfig); } /** 从缓存中获取快照,若无则返回nul */ public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null { if (!route.routeConfig || !SimpleReuseStrategy.handlers.has(route.routeConfig)) return null ; return SimpleReuseStrategy.handlers. get (route.routeConfig)!; } /** 进入路由触发,判断是否同一路由 */ public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { return future.routeConfig === curr.routeConfig; } /** 清除缓存 */ public clearCacheHandle() { SimpleReuseStrategy.handlers.forEach(item => { this .deactivateOutlet(item); }); } /** 处理同一组件不同链接的问题 */ private deactivateOutlet(handle?: DetachedRouteHandle | any): void | null { const componentRef: ComponentRef<any> = handle ? handle[ 'componentRef' ] : null ; if (componentRef) { componentRef.destroy(); } } /** * 清除单个路由 * @param url */ public clearCacheByUrl(url: string ) { var route = url.substring(url.lastIndexOf( '/' ) + 1); SimpleReuseStrategy.handlers.forEach((value: DetachedRouteHandle, key: Route) => { if (route == key.path) { SimpleReuseStrategy.handlers.delete(key); } }); } /**清除所有 */ public clearCacheAll() { console.log( "clearCacheAll" ); SimpleReuseStrategy.handlers.clear(); } /**保留一个 */ public clearCacheLeftOne(url: string ) { console.log( "clearCacheLeftOne" ); var route = url.substring(url.lastIndexOf( '/' ) + 1); SimpleReuseStrategy.handlers.forEach((value: DetachedRouteHandle, key: Route) => { if (route!= key.path) { SimpleReuseStrategy.handlers.delete(key); } }); } /** * 清除多个 * @param url */ public clearCacheByUrls(urlAry: Array< string >) { console.log( "clearCacheByUrls" ); if (urlAry && urlAry.length > 0) { let handleUrlAry: Array< string > = []; urlAry.forEach(url => { const handleUrl = url.substring(url.lastIndexOf( '/' ) + 1); handleUrlAry.push(handleUrl); }); SimpleReuseStrategy.handlers.forEach((value: DetachedRouteHandle, key: Route) => { if (key.path&&handleUrlAry.indexOf(key.path)>-1) { SimpleReuseStrategy.handlers.delete(key); } }); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现