如何在 angular2 新路由器中访问“canDeactivate”守卫中的下一条路线信息
最佳答案
我一直被困在完全相同的问题上。在我的例子中,我的 canDeactivate 逻辑将有条件地取消/重定向下一条路线。在这一点上,希望有更好的方法,我正在使用以下解决方案
- 在我的组件中,连接到
Router
事件(特别是RoutesRecognized
)以记录路线供以后使用。 - 执行
canDeactivate
并使用保存的路由数据返回 true 或 false。
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;
}
}
(注意:您无法运行代码段,但出于某种原因,代码示例格式无法正确呈现。)
我真的,真的不喜欢这个,但还没有找到任何其他方法。
关于angular - 在新的angular2路由器中访问canDeactivate guard中的下一条路由信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38465667/