2016-11-28 193 views
2

我有2个关于角2点路由器的路径问题,我花了一些时间google搜索一下,但没有运气,反正我有以下的路线设置:角2路由器路径

{ path: 'contract', component: ContractInsertUpdateComponent, children: [ 
     { path: '' }, 
     { path: ':id', component: ContractInsertUpdateComponent, children:[ 
        { path: 'buyers', component : ContractTabBuyerComponent }, 
        { path: 'seller', component : ContractTabSellerComponent } 
     ]}     
]} 

首先,让我解释一下我想在这里实现的目标,我希望使用相同的组件来插入/更新合同。我也有更多的孩子路线和完整的URL看起来应该像

localhost:4200/contract/2/buyers

  1. 我很好奇的第一件事情就是合同的默认路由

    {路径:“”}

如果我理解的话,如果路线是类似的

localhost:4200/contract

它应该加载ContractInsertUpdateComponent,它的atm,我的问题是:这是否正确的方式来做到这一点?如果可能的话,我也想避免使用空的组件作为默认路由。

    此路径设定目前不工作的
  1. 休息,例如,如果i型像

localhost:4200/contract/2

即时得到错误:无法匹配任何路由。 URL段:'contract/2'

在我的理解中,它应该加载ContractInsertUpdateComonent我错了吗?

我不知道在哪里寻找帮助,我需要一个人指点我正确的方向......感谢您的帮助!

回答

2

`/合同/ 2场比赛这条路

{ path: '' }, 

因为/contract/2开始与''(实际上每路线一样) ,然后搜索这条路线的子路由,但因为有没有失败。

{ path: '', pathMatch: 'full' }, 

应该修复它,因为那样的话,路由器不会搜索以''启动路线,但仅适用于该航线是''

更新

{ path: 'contract', component: ContractInsertUpdateComponent, children: [ 
    { path: '', pathMatch:'full', /* I guess you want some `component: ...` or `redirectTo: ...` here }, 
    { path: ':id', children:[ 
     { path: '', pathMatch:'full' }, 
     { path: 'seller', component : ContractTabSellerComponent } 
    ]} 
]} 
+0

好了,谢谢,使用你的逻辑,我设法使它工作(种) - 我在两条儿童路线上都加了'{path:'',pathMatch:'full'}',所以现在看起来像这样: {{path:'contract',component:ContractInsertUpdateComponent,children:[ {path:'',pathMatch:'full'}, {path:':id',children:{path:'',pathMatch :'full'}, {path:'seller',component:ContractTabSellerComponent} ]} ]} 现在我有一个问题,因为我的ContractInsertUpdateComponent param(id)值是NaN。任何想法? – Exerlol

+0

'id'只传递给'ContractTabSellerComponent'。您可以在那里使用它或将其传递给父项。还有一种方法可以从父级参数中找到子路由,但似乎有点脆弱。 –

+0

另请参阅http://stackoverflow.com/questions/38956129/angular-2-new-router-how-to-get-router-parameters-of-a-child-component为后面的方法 –