如何得到当前路线

当前文档仅讨论获取路径参数,而不是实际的路径段。 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

相关文章
相关标签/搜索