目的

离开页面时,做出逻辑判断

以ng-alain的项目为基础做演示

效果如图:

关键代码

定义一个CanDeactivateGuardService

export class CanDeactivateGuardService implements CanDeactivate<CanDeactivateComponent> {

    canDeactivate(component: CanDeactivateComponent,
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | boolean {
return component.leaveTip();
}
}

在component中完成leaveTip方法

leaveTip() {
return Observable.create((observer) => {
if(!this.isSave){
this.modalService.confirm({
nzTitle: '页面离开提示',
nzContent: '数据尚未保存,是否离开该页面?',
nzOnOk: async () => {
observer.next(true);
},
nzOnCancel: () => {
observer.next(false);
}
});
}
else{
observer.next(true);
}
});
}

设置ng-alain的ReuseTabMatchMode,排除can-deactivate这个目标路由

this.reuseTabService.mode = ReuseTabMatchMode.URL;
const excludes = new Array<RegExp>();
excludes.push(new RegExp('/can-deactivate'));
this.reuseTabService.excludes = excludes;

示例代码

示例代码

参考资料

feat(abc: reuse-tab): new reuse-tab component plans!

拓展阅读

Angular路由守卫

最新文章

  1. 已经重写,源码和文章请跳转http://www.cnblogs.com/ymnets/p/5621706.html
  2. 广州PostgreSQL用户会技术交流会小记 2015-9-19
  3. Java设计模式(十二) 策略模式
  4. &lt;&lt;Numerical Analysis&gt;&gt;笔记
  5. [转载]:Fortran字符串的故事
  6. Eclipse使用代码清理功能(Clean Up)
  7. Raspberry Pi3 ~ 安装samba服务
  8. Android学习笔记⑦——UI组件的学习AdapterView相关1
  9. 8款超酷而实用的CSS3按钮动画
  10. poj2750Potted Flower (线段树)
  11. VC模拟发送数据包-百度关键词查找
  12. linux shell 终端中文乱码(转)
  13. 是什么让我想到开发NFinal
  14. Python中的元类(metaclass)
  15. 如何将外部数据库 导入到系统的SQL中
  16. 【Python 11】汇率兑换4.0(函数)
  17. linux中的pwd
  18. linux 设置用户自动登出时间
  19. 固态硬盘使用简要手册&mdash;&mdash;windows平台
  20. .NET MVC下的日志文件生成法

热门文章

  1. WEB应用图片的格式,以及各自的特点和优化(一) by FungLeo
  2. 【C++竞赛 A】xxx的项链
  3. 封装springmvc处理ajax请求结果
  4. C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。
  5. [Django] Creating an app, models and database
  6. KVC设置系统自带属性,不管是不是私有的属性
  7. C# Tuple VS ValueTuple
  8. 关于java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.makeAccessible
  9. Lucene分词报错:”TokenStream contract violation: close() call missing”
  10. JSON排序