【问题标题】:How to specify auxiliary component for child route in angular2 router3如何在angular2 router3中为子路由指定辅助组件
【发布时间】:2017-01-20 02:50:04
【问题描述】:

我正在使用新的 route3,想知道为子路由指定辅助组件的 URL 语法是什么。

我有以下子路由定义:

const router: RouterConfig = [
  {
    path: 'component-c', component: ComponentC, children: [
      { path: 'auxPath', component: ComponentAux, outlet: 'aux'},
      { path: 'c1', component: ComponentC1 }
    ]
  }
];

而ComponentC的模板是这样的

@Component({
  selector: 'component-c',
  template: `
    <div>Child Route Component</div>
    <router-outlet></router-outlet>
    <router-outlet name="aux"></router-outlet>
  `,
  directives: [ROUTER_DIRECTIVES]
})

URL /component-c/c1 显示 ComponentC1;但是,我尝试了许多组合,例如 /component-c(aux:auxPath)/c1/component-c/c1(aux: auxPath) 等,但仍然无法弄清楚如何让 ComponentAux 显示出来......

【问题讨论】:

    标签: angular angular2-routing angular2-router3


    【解决方案1】:

    您应该尝试如下形成您的网址。

    this.router.navigateByUrl('/component-c/(c1//aux:auxPath)
    

    根据我上次的调查,当前路由器版本在使用routerLinknavigate 方法导航到辅助插座时存在一些问题。您应该构建完整的 URL 并使用 navigateByUrl

    Plunker 与 example。单击详细信息按钮,然后单击管理员。管理员在管理员插座中路由。全屏打开以查看 URL 格式。

    URL 具有以下语义

    1. 子路径由 / 分隔
    2. 如果一个路径有兄弟,那么它应该用括号 () 括起来
    3. 兄弟姐妹用 // 分隔
    4. 出口和路径由:分隔:

      Parent/child/(gchild1//outletname:gchild2)

    另外一个question

    【讨论】:

    • 有效!也感谢您的提示,我确实遇到了与 routerLink 和导航方法相同的问题。
    • @Arpit 您可以将此问题标记为重复答案,我看到链接的答案几乎相同:)
    • 确实,我的答案都是针对提问者的要求进行了调整。下次我会看到的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 2016-05-20
    • 2019-02-19
    相关资源
    最近更新 更多