访问下面的路径信息canDeactivate在新的angular2路由器中

Vin*_*onz 5 router angular

如何在angular2新路由器中的"canDeactivate"保护中访问下一个路由信息

Jas*_*son 0

我一直被同样的问题困扰。就我而言,我的 canDeactivate 逻辑将有条件地取消/重定向下一条路线。此时,希望有更好的方法,我正在使用以下解决方案

  1. 从我的组件中,挂钩Router事件(特别是RoutesRecognized)以记录路线以供以后使用。
  2. 实现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;
	}
}
Run Code Online (Sandbox Code Playgroud)

(注意:您无法运行该代码片段,但由于某种原因,代码示例格式无法正确呈现。)

我真的非常不喜欢这个,但还没有找到任何其他方法。