simpleReuseStrategy.ts

// 创建重用策略
import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router'; /**
* 路由重用策略
*/
export class SimpleReuseStrategy implements RouteReuseStrategy { // 保存路由快照
// [key:string] 键为字符串类型
// DetachedRouteHandle 值为路由处理器
// public handlers: { [key: string]: DetachedRouteHandle } = {};
public static handlers: { [key: string]: DetachedRouteHandle } = {}; public static deleteRouteSnapshot(path?: string): void {
if (!path) {
SimpleReuseStrategy.handlers = {};
return;
}
const name = path.replace(/\//g, '_');
if (SimpleReuseStrategy.handlers[name]) {
delete SimpleReuseStrategy.handlers[name];
}
}
/**
* 从缓存中获取快照
* @param {ActivatedRouteSnapshot} route
* @return {DetachedRouteHandle | null}
*/
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null {
return SimpleReuseStrategy.handlers[route.routeConfig.path];
} /**
* 是否允许还原
* @param {ActivatedRouteSnapshot} route
* @return {boolean} true-允许还原
*/
shouldAttach(route: ActivatedRouteSnapshot): boolean {
return !!route.routeConfig && !! SimpleReuseStrategy.handlers[route.routeConfig.path];
} /**
* 确定是否应该分离此路由(及其子树)以便以后重用
* @param {ActivatedRouteSnapshot} route
* @return {boolean}
*/
shouldDetach(route: ActivatedRouteSnapshot): boolean {
return route.data.reload === false;
} /**
* 进入路由触发, 判断是否为同一路由
* @param {ActivatedRouteSnapshot} future
* @param {ActivatedRouteSnapshot} curr
* @return {boolean}
*/
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
// future - 未来的(下一个)路由快照
return future.routeConfig === curr.routeConfig;
} /**
* 保存路由
* @param {ActivatedRouteSnapshot} route
* @param {DetachedRouteHandle | null} handle
*/
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle | null): void {
// 通过 Route.path 映射路由快照, 一定要确保它的唯一性
// 也可以通过 route.routeConfig.data.uid 或其他可以确定唯一性的数据作为映射key
// 作者这里能够确保 path 的唯一性
SimpleReuseStrategy.handlers[route.routeConfig.path] = handle;
} }

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

 providers: [
{ provide: RouteReuseStrategy, useClass: SimpleReuseStrategy },
{ provide: NZ_I18N, useValue: en_US },
ConfigService, HttpInterceptorProviders
],

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

const routes: Routes = [
{
path: '',
component: AuthorityManagementComponent,
children: [
{ path: '', redirectTo: 'authority-manage', pathMatch: 'full' },
{ path: 'authority-manage', component: AuthorityManageComponent, data: {reload: false} }, // 路由重载
{ path: 'add-authority', component: AddAuthorityComponent, data: {reload: false} },
{ path: 'follow-setting', component: FollowSettingComponent, data: {reload: false} },
{ path: 'order-setting', component: OrderSettingComponent, data: {reload: false} },
{ path: 'refund-setting', component: RefundSettingComponent, data: {reload: false} },
{ path: 'sales-setting', component: SalesSettingComponent, data: {reload: false} }
]
}
];

关于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. mysql的show status和show global status区别在哪

    show status                   本次会话的参数状态show global status        本次MYSQL服务开启(或重置)到现在总请求数

  2. 关于图算法 & 图分析的基础知识概览

    网址:https://learning.oreilly.com/library/view/graph-algorithms-/9781492060116/ 你肯定没有读过这本书,因为这本书的发布日期是 ...

  3. 32. docker swarm 集群服务通信 之 RoutingMesh - internal 网络

    1. 两个 service 是如何通信的 通过内置的 DNS 服务发现的功能  相互通信的 2. 创建一个 overlay 的 网络 docker network create -d overlay ...

  4. jquery 第一节 什么是jQuery

    简单来说,jQuery就是javascript的一个框架,也可以说是javascript的一个库.

  5. 手机H5,用Jquery使图片自动填满两栏式排版

    遇上这样的排版,手机的解象度都不同,假如只用CSS3根本就做不出这样的排版:因此要用Jquery. 1. HTML <div class="postImgCenterCrop" ...

  6. 一维跳棋(BFS)

    一维跳棋是一种在1×(2N+1) 的棋盘上玩的游戏.一共有N个棋子,其中N 个是黑的,N 个是白的.游戏开始前,N 个白棋子被放在一头,N 个黑棋子被放在另一头,中间的格子空着. 在这个游戏里有两种移 ...

  7. 编程作业3.1:Multi-class classification(One-vs-all)

    题目: 在本次练习中,你将使用逻辑回归和神经网络来识别手写数字(从0到9). 今天,自动手写数字识别被广泛使用,从识别信封上的邮政编码到识别银行支票上的金额.这个练习将向你展示如何将你所学的方法用于此 ...

  8. 17.3.12---sys模块

    1---sys是system的缩写,用来获取操作系统和编译器的一些配置,设置和操作,如判断文件和文件夹是否存在,创建文件夹,获取系统版本之类的操作 import sys  #导入sys模块 2--一些 ...

  9. ZJNU 1528 - War--高级

    类似于1213取水 可以把空投当作第0个城市 最后将0~n的所有城市跑最小生成树 /* Written By StelaYuri */ #include<iostream> #includ ...

  10. hdu 1246

    很久没有写题解了~因为懒(年纪大了就是脸皮厚,还有脸说) 这道题今天花了很长时间去推,一开始以为是规律题,没推出来,直接模拟也TLE了,接着考虑实在是没思路,看了题解. 思路大概就是这样: 先上代码( ...