NG RouteReuseStrategy(路由复用策略)
路由: |
|
|
|
路由的本质: |
|
|
|
RouteReuseStrategy(路由复用策略) |
|
|
|
基础的路由复用策略 |
|
// 基础的路由复用策略 import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router'; export class SimpleReuseStrategy implements RouteReuseStrategy { _cacheRouters: { [key: string]: any } = {}; // 存储路由快照 /** * @description: 路由是否允许复用 * @param {type} route * @return: boolean true-允许 false-禁止 */ shouldDetach(route: ActivatedRouteSnapshot): boolean { return true; } /** * @description: 存储路由快照,路由离开时,触发; * 按path作为key存储路由快照&组件当前实例对象 * path等同RouterModule.forRoot中的配置 * @param {type} */ store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void { this._cacheRouters[route.routeConfig.path] = { snapshot: route,handle: handle,}; } /** * @description: 是否允许还原路由快照 * 在缓存中有的都认为允许还原路由 * @param {type} route * @return:boolean true-允许 false-禁止 */ shouldAttach(route: ActivatedRouteSnapshot): boolean { // 在路由是login的时候清空缓存 if(route.routeConfig['path'] === 'login'){ this._cacheRouters = {}; } return !!route.routeConfig && !!this._cacheRouters[route.routeConfig.path]; } /** * @description: 获取存储的路由 * 从缓存中获取快照 * @param {type} route * @return:若无则返回null */ retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { if (!route.routeConfig || !this._cacheRouters[route.routeConfig.path]) return null; return this._cacheRouters[route.routeConfig.path].handle; } /** * @description: 同一路由时,复用路由;进入路由触发 * @param {type} * @return:boolean true-复用 false-不复用 */ shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { return future.routeConfig === curr.routeConfig; } } |
|
注册 |
|
// 将策略注册到模块当中 providers: [ { provide: RouteReuseStrategy, useClass: SimpleReuseStrategy } ] |
|
删除路由快照 |
|
// 组件导入 providers: [SimpleReuseStrategy] module => 存储的key delete SimpleReuseStrategy._cacheRouters[module]; |
|
懒加载情况下,复用策略失效; |
|
|
|
使用路由复用策略后ngOnInit 的方法不会再执行 |
|
import {NavigationEnd, Router} from '@angular/router'; constructor(private router: Router) { this.router.events.filter((event) => event instanceof NavigationEnd).subscribe((event: NavigationEnd) => { // 这里需要判断一下当前路由,如果不加的话,每次路由结束的时候都会执行这里的方法,这里以search组件为例 if (event.url === '/search') { this.search(); // 在这写需要执行初始化的方法 } }); } |
参考地址 |
||
Ng-Alain reuse-tab |
https://github.com/ng-alain/delon/blob/master/packages/abc/reuse-tab/reuse-tab.service.ts |
|
复杂路由复用策略 |
https://github.com/angular/angular/issues/13869#issuecomment-441054267 |
NG RouteReuseStrategy(路由复用策略)相关推荐
- angular 路由复用策略
最近在弄单页面组件刷新功能的时候,接触到路由复用策略,总结下: 用法: 新建路由复用文件 SimpleReuseStrategy.ts: import { RouteReuseStrategy, De ...
- Angular6路由复用策略解读
本文除了介绍路由复用策略外,还实现了父子路由如何复用的问题,项目见末尾: 1.父路由和子路由页面的tab不应该同时存在,否则,切换时容易报以下错误: Cannot reattach Activated ...
- Angular路由复用策略出现Cannot reattach ActivatedRouteSnapshot created from a different route错误
复用代码 自己写的angular路由复用,用着基本挺好. 定义的的顺序就是复用的逻辑,离开某路由页面时,shouldDetach觉得可复用后,store去保存路由,再次进入页面时,shouldAtta ...
- Ant Design Blazor 组件库的路由复用多标签页介绍
前言 Blazor 是 .NET 最新的前端框架,可以基于 WebAssembly 或 SignalR (WebSocket)构建前端应用程序,基于 WebAssembly 托管模型的 Blazor ...
- 计算机网络最佳路由,优化计算机网络路由的策略
优化计算机网络路由的策略 计算机网络技术的广泛应用为网络路由的发展带来新的机遇与挑战,对提高网络资源的应用效率具有重要意 (本文共2页) 阅读全文>> 随着移动互联网技术和多媒体应用的快速 ...
- NetScreen下“区域”与“路由”、“策略”基本概念
NetScreen下"区域"与"路由"."策略"基本概念 "区域"这个概念是NetScreen自己定的,它是很多NetS ...
- router-view添加key解决同一路由复用相同组件
router-view添加key解决同一路由复用相同组件,比如/page/1.page/2就会复用组件,不执行钩子函数 <router-view :key="$route.fullPa ...
- Vue--Router--解决多路由复用同一组件页面不刷新问题
原文网址:Vue--Router--解决多路由复用同一组件页面不刷新问题_IT利刃出鞘的博客-CSDN博客 简介 说明 本文介绍如何解决Vue的多路由复用同一组件页面不刷新问题. 多路由复用同一组件的 ...
- 【Android 内存优化】Bitmap 内存缓存 ( Bitmap 内存复用 | 弱引用 | 引用队列 | 针对不同 Android 版本开发不同的 Bitmap 复用策略 | 工具类代码 )
文章目录 一.Bitmap 复用池 二.弱引用 Bitmap 内存释放 三.从 Bitmap 复用池中获取对应可以被复用的 Bitmap 对象 1.Android 2.3.3(API 级别 10)及以 ...
最新文章
- UWP入门(八)--几个简单的控件
- [转载]基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
- 【从传统方法到深度学习】图像分类
- android图片浏览远近,快图浏览编辑图片方法介绍_怎么编辑图片_3DM手游
- 【Java】while(scanner.hasNext()){}引发“死循环”的处理方法
- python3.3使用tkinter实现猜数字游戏代码
- 使命召唤12服务器系统,《使命召唤12》平衡技能系统 排位赛有专属服务器
- 果断 Mark!27 个免费、低成本 Python 学习资源入手!
- Clojure 学习入门(7)- 连接mysql
- HBase简介及使用
- java po vo bo是什么以及_Java中VO , PO , BO, DAO ,POJO是什么意思
- Godaddy SSL证书解析到阿里云后配置nginx服务器https
- 龙的结构图解_地下城与龙的最佳数字工具
- 【Unity小功能开发实战教程】制作跟随倒计时变化的进度条
- 课后实践10:以闲鱼为例,梳理产品功能结构迭代路径
- 【Python标准库】base64模块
- windows server2012 r2修改用户名
- Python ancii 转utf-8 失败
- 盘点几种主流LED驱动方式及驱动电源
- git设置http和https代理
热门文章
- ajax-loader.gif不存在怎么解决_高温下水泥路面材料膨胀路面上出现裂缝怎么处理?...
- es6箭头函数_javascript-ES6函数进阶(箭头函数,默认参数)(笔记)
- 解决Vue开发过程中与后台服务器跨域问题
- 太沉重了:中国获全球“人道主义摄影奖”的照片!组图
- Spring数据分析思维课
- 那些查了无数遍的问题
- [改善Java代码]三元操作符的类型务必一致
- 关于android Intent意图的一点心得
- Flutter异步加载FutureBuilder重绘解决方案
- Android 屏幕适配攻略(四)获取手机屏幕的相关信息 与动态设置控件的大小