2017-09-21 50 views
1

如何提取的孩子我訪問我的應用程序的頁面:在出口

myApp.com/#/accident/(carsContainer:robustness)

在事故成分,我想提取「健壯性」部分。我想我必須使用ActivatedRoute,但找不到方法

回答

1

accident組件中,您可以訂閱路由器事件並獲取url,然後從中提取所需的任何內容。像這樣:

import { Router, NavigationEnd, ActivatedRoute } from '@angular/router'; 

constructor(private activatedRoute: ActivatedRoute, private router: Router){} 

ngOnInit() { 
    this.router.events 
     .filter((event) => event instanceof NavigationEnd) 
     .map(() => this.activatedRoute) 
     .map((route) => { 
      while (route.firstChild) route = route.firstChild; 
      console.log(route.snapshot.url[0].path) 
      return route; 
     }) 
     .subscribe((event) => { 
      console.log('NavigationEnd:', event); 
     }); 
} 
+1

這是完美的,thx :) – Lempkin