當前文檔僅討論獲取路徑參數,而不是實際的路徑段。 ide
例如,若是我想找到當前路線的父路線,那怎麼可能? this
向您的組件中注入Location
並讀取location.path();
您須要在某處添加
您須要將ROUTER_DIRECTIVES
,以便Angular能夠解析
Location
。
import: [RouterModule]
添加到模塊中。 google
更新資料 url
在V3(RC.3)路由器中,您能夠注入ActivatedRoute
並使用其snapshot
屬性訪問更多詳細信息。 spa
constructor(private route:ActivatedRoute) { console.log(route); }
要麼 code
constructor(private router:Router) { router.events.subscribe(...); }
另請參閱Angular 2路由器事件偵聽器 router
在Angular2 Rc1中,您能夠注入RouteSegment並以naviagte方法傳遞它們。 事件
constructor(private router:Router,private segment:RouteSegment) {} ngOnInit() { this.router.navigate(["explore"],this.segment) }
角2 rc2 路由
router.urlTree.contains(router.createUrlTree(['/home']))
新的V3路由器具備url屬性。 文檔
this.router.url === '/login'
您可使用ActivatedRoute
獲取當前路由器
原始答案(RC版)
我在AngularJS Google Group上找到了一個解決方案,它是如此簡單!
ngOnInit() { this.router.subscribe((url) => console.log(url)); }
這是原始答案
https://groups.google.com/d/msg/angular/wn1h0JPrF48/zl1sHJxbCQAJ