simpleReuseStrategy.ts

  1. // 创建重用策略
  2. import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router';
  3. /**
  4. * 路由重用策略
  5. */
  6. export class SimpleReuseStrategy implements RouteReuseStrategy {
  7. // 保存路由快照
  8. // [key:string] 键为字符串类型
  9. // DetachedRouteHandle 值为路由处理器
  10. // public handlers: { [key: string]: DetachedRouteHandle } = {};
  11. public static handlers: { [key: string]: DetachedRouteHandle } = {};
  12. public static deleteRouteSnapshot(path?: string): void {
  13. if (!path) {
  14. SimpleReuseStrategy.handlers = {};
  15. return;
  16. }
  17. const name = path.replace(/\//g, '_');
  18. if (SimpleReuseStrategy.handlers[name]) {
  19. delete SimpleReuseStrategy.handlers[name];
  20. }
  21. }
  22. /**
  23. * 从缓存中获取快照
  24. * @param {ActivatedRouteSnapshot} route
  25. * @return {DetachedRouteHandle | null}
  26. */
  27. retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null {
  28. return SimpleReuseStrategy.handlers[route.routeConfig.path];
  29. }
  30. /**
  31. * 是否允许还原
  32. * @param {ActivatedRouteSnapshot} route
  33. * @return {boolean} true-允许还原
  34. */
  35. shouldAttach(route: ActivatedRouteSnapshot): boolean {
  36. return !!route.routeConfig && !! SimpleReuseStrategy.handlers[route.routeConfig.path];
  37. }
  38. /**
  39. * 确定是否应该分离此路由(及其子树)以便以后重用
  40. * @param {ActivatedRouteSnapshot} route
  41. * @return {boolean}
  42. */
  43. shouldDetach(route: ActivatedRouteSnapshot): boolean {
  44. return route.data.reload === false;
  45. }
  46. /**
  47. * 进入路由触发, 判断是否为同一路由
  48. * @param {ActivatedRouteSnapshot} future
  49. * @param {ActivatedRouteSnapshot} curr
  50. * @return {boolean}
  51. */
  52. shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
  53. // future - 未来的(下一个)路由快照
  54. return future.routeConfig === curr.routeConfig;
  55. }
  56. /**
  57. * 保存路由
  58. * @param {ActivatedRouteSnapshot} route
  59. * @param {DetachedRouteHandle | null} handle
  60. */
  61. store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle | null): void {
  62. // 通过 Route.path 映射路由快照, 一定要确保它的唯一性
  63. // 也可以通过 route.routeConfig.data.uid 或其他可以确定唯一性的数据作为映射key
  64. // 作者这里能够确保 path 的唯一性
  65. SimpleReuseStrategy.handlers[route.routeConfig.path] = handle;
  66. }
  67. }

在app.module.ts里面引入和配置

  1. providers: [
  2. { provide: RouteReuseStrategy, useClass: SimpleReuseStrategy },
  3. { provide: NZ_I18N, useValue: en_US },
  4. ConfigService, HttpInterceptorProviders
  5. ],

在相应模块的module里面路由配置末尾加上data: {reload: false}实现路由重载

  1. const routes: Routes = [
  2. {
  3. path: '',
  4. component: AuthorityManagementComponent,
  5. children: [
  6. { path: '', redirectTo: 'authority-manage', pathMatch: 'full' },
  7. { path: 'authority-manage', component: AuthorityManageComponent, data: {reload: false} }, // 路由重载
  8. { path: 'add-authority', component: AddAuthorityComponent, data: {reload: false} },
  9. { path: 'follow-setting', component: FollowSettingComponent, data: {reload: false} },
  10. { path: 'order-setting', component: OrderSettingComponent, data: {reload: false} },
  11. { path: 'refund-setting', component: RefundSettingComponent, data: {reload: false} },
  12. { path: 'sales-setting', component: SalesSettingComponent, data: {reload: false} }
  13. ]
  14. }
  15. ];

关于angular2跳路由防止页面刷新的做法(Angular2路由重载)的更多相关文章

  1. Vue 实现动态路由及登录&404页面跳转控制&页面刷新空白解决方案

    Vue实现动态路由及登录&404页面跳转控制&页面刷新空白解决方案   by:授客 QQ:1033553122   开发环境   Win 10   Vue 2.9.6   node-v ...

  2. react-navigation的多次点击重复跳转同一页面、不在堆栈路由页面使用navigation方法的解决思路

    一.react-navigation的初使用 createStackNavigator  ==> createSwitchNavigator  ==>  createAppContaine ...

  3. Vue实现远程获取路由与页面刷新导致404错误的解决

    一.背景 先简单介绍一下现在项目情况:前后端分离,后端服务是Java写的,前端是Vue+ElementUI. 最近的一个需求是:通过后端Api去获取前端路由表,原因是每个登录角色对应的前端路由表可能是 ...

  4. vue通过路由实现页面刷新

    vue 开发微信商城项目,需求如下: 购物车页面跳转到详情页,购物车页面包含了多个组件,点击结算跳转到订单页面,从订单返回时,购物车页面没有刷新,由于购物车组件之间通过bus实现事件传递,页面跳转(非 ...

  5. vue 键盘回车事件导致页面刷新的问题,路由多了一个问号

    问题: <el-form @submit.native.prevent> <el-form-item > <el-input @keyup.enter.native=&q ...

  6. vue路由传参刷新丢失

    没有系统学习过vue,以前使用路由传参都是直接this.$router.push({name:'main',params:{'id': 123}})的,没有在路由定义中配置参数,如下: router: ...

  7. vue爬坑之路1-路由跳转全新页面以及二级页面配置

    之前也在网找了一些答案,比较零碎,特此总结下,废话不多说,直接上干货! 路由跳转全新页面 首先在app.vue中先定义router-view,这是主路由. 在router的index.js中引入log ...

  8. 解决angular2页面刷新后报404错误

    如果你的angular项目部署到一个tomcat容器里面,localhost:8080是JavaWeb的主页,localhost:8080/driver/login是你angular2项目的登陆地址. ...

  9. 直击--vue项目微信小程序页面跳转web-view不刷新-根源

    背景 最近项目需要适配小程序,项目是使用了vue开发的网站,其中改造方式是,每个页面都使用小程序创建一个页面通过web-view来显示指定页面的. 在没有使用小程序时,路由跳转时,刷新页面等等,这个是 ...

随机推荐

  1. Window Mysql5.7免安装版配置

    1.下载mysql 5.7 32位:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-win32.zip 5.7 64位:https ...

  2. ruoyi ShiroUtils

    package com.ruoyi.framework.util; import org.apache.shiro.SecurityUtils; import org.apache.shiro.cry ...

  3. 第4章 ZK基本特性与基于Linux的ZK客户端命令行学习

    第4章 ZK基本特性与基于Linux的ZK客户端命令行学习 4-1 zookeeper常用命令行操作 4-2 session的基本原理与create命令的使用

  4. usert

    usert类型 不是一个函数,而是一个语言构造器 usert后会不会释放内存 当usert的文件大于2044KB时才会释放内存,否则不释放内存

  5. 在mybatis框架中,延迟加载与连表查询的差异

    1.引子 mybatis的延迟加载,主要应用于一个实体类中有复杂数据类型的属性,包括一对一和一对多的关系(在xml中用collection.association标签标识).这个种属性往往还对应着另一 ...

  6. Educational Codeforces Round 55 (Rated for Div. 2)E

    题:https://codeforces.com/contest/1082/problem/E 题意:给出n个数和一个数c,只能操作一次将[L,R]之间的数+任意数,问最后该序列中能存在最多多少个c ...

  7. 形参和实参|默认值|可选实参|tuple|*tuple|args|*args | **kwargs|args[:]|

    #!/usr/bin/python def hello(i,greet='long time to see!'): out = "hello "+i+" "+g ...

  8. @interface 注解详解

    转:http://www.cnblogs.com/xdp-gacl/p/3622275.html 只为成功找方法,不为失败找借口! Java基础加强总结(一)——注解(Annotation) 一.认识 ...

  9. 项目server中设置session timeout遇到的问题

    RT:在项目server中的web.xml设置session timeout=10,当10分钟后,继续右键执行jsp文件,运行失败,如下图所示: 但是单独启动tomcat server后,在浏览器中输 ...

  10. cmd释放重新获取IP

    1.打开电脑的命令提示符运行设置窗口之后,我们收入  ipconfig/release  ,然后点击回车键  ,释放之前获取的IP地址 2.释放之前的IP地址之后,我们在输入  ipconfig/re ...