【问题标题】:How can I acces a route parameter two parents up in Angular 11?如何在 Angular 11 中访问两个父级的路由参数?
【发布时间】:2021-05-14 11:43:52
【问题描述】:

我想知道如何以角度访问杠杆两条父路线。例如:如果我使用以下路由,如何在 AttributeListComponent 中获取 componentId

const routes: Routes = [
  {path: '', redirectTo: '/components', pathMatch: 'full'},
  {path: 'components', component: SideNavComponent,
    children: [
      {path: ':componentId', component: ComponentTabComponent, children: [
          {path: '', redirectTo: 'entities', pathMatch: 'full'},
          {path: 'entities', component: EntityListComponent, children: [
              {path: ':entityId', component: EntityTabComponent,
                children: [
                  {path: '', redirectTo: 'attributes', pathMatch: 'full'},
                  {path: 'attributes', component: AttributeListComponent},
                  {path: 'tasks', component: TaskListComponent}
                ]}
            ]},
          {path: 'domains', component: DomainListComponent}
        ]},
    ]},
  { path: '**', component: PageNotFoundComponent }
];

【问题讨论】:

  • 举例说明你如何/为什么要使用这个想法。

标签: html css json angular typescript


【解决方案1】:

您可以使用parent 属性向上遍历路由器树。示例:

  constructor(private route: ActivatedRoute) {}
  ...
  this.route.parent.parent.parent.queryParams.subscribe(...

计算您自己需要多少parent :)。这取决于您在路由文件中有多少 children

【讨论】:

  • 谢谢!我没找到。
猜你喜欢
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 2016-11-25
相关资源
最近更新 更多