【问题标题】:Access next route information inside canDeactivate guard in new angular2 router在新的angular2路由器中访问canDeactivate guard中的下一条路由信息
【发布时间】:2016-07-19 18:11:18
【问题描述】:

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

【问题讨论】:

    标签: angular router


    【解决方案1】:

    我一直被困在完全相同的问题上。就我而言,我的 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;
    	}
    }

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

    我真的,真的不喜欢这样,但还没有找到其他方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      • 2016-11-29
      • 1970-01-01
      相关资源
      最近更新 更多