2017-06-14 36 views
0

這可能是重複的,但我找不到答案。帶有參數錯誤的angular2路由器

當我嘗試使用參數導航到路由器時,出現以下錯誤:Error: Cannot match any routes. URL Segment: 'order/24'

我的路由器配置爲:

const routes: Routes = [ 
    { 
    path: 'pm', component: PmComponent, canActivateChild: [AuthGuard], 
    children: [ 
     {path: 'orderlist', component: OrderlistComponent}, 
     {path: 'new-order', component: NewOrderComponent}, 
     {path: 'order/:id', component: OrderComponent}, 
     {path: '**', component: PmDefaultComponent} 
    ] 
    } 
]; 
new-order組件我嘗試導航到 order/:id

this.router.navigate(['order/', this.orderId]); 

我已經嘗試

this.router.navigate(['/order/', this.orderId]); 

感謝您的幫助。

+0

您是否嘗試過重新啓動CLI? –

+0

是的,我試過這個 – Lyuba

回答

2

問題是你定義path: 'pm', component: .. ,所以這條路線的所有孩子必須以pm開頭。嘗試導航到像這樣的路由:

this.router.navigate(['/pm/order', this.orderId]); 
+0

謝謝,這工作)你已經救了我的傍晚:) – Lyuba

+0

@Lyuba歡迎您 – lomboboo

1

嘗試用

this.router.navigate(['./order', this.orderId],{ 
relativeTo: this.route 
}); 

this.route是注入ActivatedRoute實例。或者你可以用絕對路徑選項this.router.navigate(['/pm/order', this.orderId])

  • ../意味着你路由樹去上一級
  • ./手段sybling
  • /意味着絕對路徑,this.router.navigate(['/order', this.orderId]);root/order/:id例如
+0

我試過'../'和'。/',沒有成功:'錯誤:無法匹配任何路由。 URL段:'order/26'' – Lyuba