访问下面的路径信息canDeactivate在新的angular2路由器中
发布时间:2020-12-17 18:05:00 所属栏目:安全 来源:网络整理
导读:如何在angular2新路由器中的“canDeactivate”保护中访问下一个路由信息 解决方法 我一直坚持同样的问题.在我的情况下,我的canDeactivate逻辑将有条件地取消/重定向下一个路由.在这一点上,希望有更好的方法,我使用以下解决方案 从我的组件中,挂钩到Router事
如何在angular2新路由器中的“canDeactivate”保护中访问下一个路由信息
解决方法
我一直坚持同样的问题.在我的情况下,我的canDeactivate逻辑将有条件地取消/重定向下一个路由.在这一点上,希望有更好的方法,我使用以下解决方案
>从我的组件中,挂钩到Router事件(特别是RoutesRecognized)以记录该路由供以后使用. ngOnInit(): void { this.tabbedSearch = this.profilesService.currentProfile.tabbedSearch; this.router.events .filter(e => e instanceof RoutesRecognized) .map(e => <RoutesRecognized> e) .subscribe((e) => { this.lastRoute = e.state.root.firstChild.routeConfig.path; this.lastParams = e.state.root.firstChild.params; }); } public canDeactivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { // do not navigate away if we are doing tabbed search AND the new route is the article details view if (this.tabbedSearch && this.lastRoute == "article/details/:type/:id/:version") { this.tabbedDetails.push(Object.create(this.lastParams)); return false; } else { return true; } } (注意:您无法运行代码段,但由于某种原因,代码示例格式化无法正确呈现.) 我真的,真的不喜欢这个,但没有找到任何其他方式. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |