簡體   English   中英

帶有參數錯誤的angular2路由器

[英]angular2 router with parameters error

這可能是重復的,但我找不到答案。

當我嘗試使用參數導航到路由器時,出現以下錯誤: Error: Cannot match any routes. URL Segment: 'order/24' 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]);

感謝幫助。

問題是您定義了path: 'pm', component: .. ,因此此路由的所有子代必須以pm開頭。 嘗試導航到這樣的路線:

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

試試看

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM