如何得到當前路線

當前文檔僅討論獲取路徑參數,而不是實際的路徑段。 ide

例如,若是我想找到當前路線的父路線,那怎麼可能? this


#1樓

向您的組件中注入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


#2樓

在Angular2 Rc1中,您能夠注入RouteSegment並以naviagte方法傳遞它們。 事件

constructor(private router:Router,private segment:RouteSegment) {}

  ngOnInit() {
    this.router.navigate(["explore"],this.segment)
  }

#3樓

角2 rc2 路由

router.urlTree.contains(router.createUrlTree(['/home']))

#4樓

新的V3路由器具備url屬性。 文檔

this.router.url === '/login'

#5樓

您可使用ActivatedRoute獲取當前路由器

原始答案(RC版)

我在AngularJS Google Group上找到了一個解決方案,它是如此簡單!

ngOnInit() {
  this.router.subscribe((url) => console.log(url));
}

這是原始答案

https://groups.google.com/d/msg/angular/wn1h0JPrF48/zl1sHJxbCQAJ

相關文章
相關標籤/搜索