Angular路由守卫 canDeactivate
2024-10-02 02:09:40
目的
离开页面时,做出逻辑判断
以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!
拓展阅读
最新文章
- 已经重写,源码和文章请跳转http://www.cnblogs.com/ymnets/p/5621706.html
- 广州PostgreSQL用户会技术交流会小记 2015-9-19
- Java设计模式(十二) 策略模式
- <;<;Numerical Analysis>;>;笔记
- [转载]:Fortran字符串的故事
- Eclipse使用代码清理功能(Clean Up)
- Raspberry Pi3 ~ 安装samba服务
- Android学习笔记⑦——UI组件的学习AdapterView相关1
- 8款超酷而实用的CSS3按钮动画
- poj2750Potted Flower (线段树)
- VC模拟发送数据包-百度关键词查找
- linux shell 终端中文乱码(转)
- 是什么让我想到开发NFinal
- Python中的元类(metaclass)
- 如何将外部数据库 导入到系统的SQL中
- 【Python 11】汇率兑换4.0(函数)
- linux中的pwd
- linux 设置用户自动登出时间
- 固态硬盘使用简要手册&mdash;&mdash;windows平台
- .NET MVC下的日志文件生成法
热门文章
- WEB应用图片的格式,以及各自的特点和优化(一) by FungLeo
- 【C++竞赛 A】xxx的项链
- 封装springmvc处理ajax请求结果
- C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。
- [Django] Creating an app, models and database
- KVC设置系统自带属性,不管是不是私有的属性
- C# Tuple VS ValueTuple
- 关于java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.makeAccessible
- Lucene分词报错:”TokenStream contract violation: close() call missing”
- JSON排序