Angular 角度路由器获取与子树匹配的URL

Angular 角度路由器获取与子树匹配的URL,angular,Angular,我想编写一个组件来处理给定URL基的所有子路径。例如: /text/123 /text/something /text/this/is/a/subpath 所以我发现我可以设置我的路由表,让所有这些URL进入我的组件,如下所示: const route: Routes = [ { path: '', component: HomeComponent }, { path: 'signup', component: SignupComponent }, { path: 'login',

我想编写一个组件来处理给定URL基的所有子路径。例如:

/text/123
/text/something
/text/this/is/a/subpath
所以我发现我可以设置我的路由表,让所有这些URL进入我的组件,如下所示:

const route: Routes = [
  { path: '', component: HomeComponent },
  { path: 'signup', component: SignupComponent },
  { path: 'login', component: LoginComponent },
  { path: 'text', component: TextComponent, children: [
      { path: '**', component: TextComponent }
    ] },
到目前为止还不错。一切都会按预期调用TextComponent。然而,我不知道如何才能最好地获得到达我的URL的完整子路径。通过在控制台中四处搜索,我发现以下方法似乎有效:

constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.url = this.route.snapshot._routerState.url;
我得到了以“/text/”开头的字符串。我可以这么说,但我找不到任何文件表明这是正确的财产使用。此外,我不确定以下划线(\u routerState在本例中)开头的属性是否打算以这种方式使用


谁能指引我走上正确的道路?(双关语)。

作为一个简单的解决方法,您可以使用
window.location.pathname
。试着在你的typescipt文件中运行它和/或操纵它以得到你想要的

console.log(window.location.pathname);
角度解

我问了几个我在NgConf认识的人返回URL的方法。您需要注入一个路由器实例,而不是ActivatedRoute

constructor(private router: Router) { }

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

我想我自己应该想到这一点,但我正在寻找一个“纯角度”的解决方案。我仍然想找到一个,但您的想法似乎至少可以用于客户端操作。@AlanObject我为您找到了一个更好的解决方案