具有参数错误的angular2路由器
angular2 router with parameters error
这可能是重复的,但我找不到答案。
当我尝试使用参数导航到路由器时,出现以下错误: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]);
感谢您的帮助。
试试
this.router.navigate(['./order', this.orderId],{
relativeTo: this.route
});
其中 this.route
是注入的 ActivatedRoute
实例。或者您可以使用 this.router.navigate(['/pm/order', this.orderId])
.
的绝对路径选项
../
表示您在路线树中向上一级
./
表示同胞
/
表示绝对路径,this.router.navigate(['/order', this.orderId]);
例如root/order/:id
问题是你定义了path: 'pm', component: ..
,所以这条路由的所有children都必须以pm
开头。尝试导航到这样的路线:
this.router.navigate(['/pm/order', this.orderId]);
这可能是重复的,但我找不到答案。
当我尝试使用参数导航到路由器时,出现以下错误: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]);
感谢您的帮助。
试试
this.router.navigate(['./order', this.orderId],{
relativeTo: this.route
});
其中 this.route
是注入的 ActivatedRoute
实例。或者您可以使用 this.router.navigate(['/pm/order', this.orderId])
.
../
表示您在路线树中向上一级./
表示同胞/
表示绝对路径,this.router.navigate(['/order', this.orderId]);
例如root/order/:id
问题是你定义了path: 'pm', component: ..
,所以这条路由的所有children都必须以pm
开头。尝试导航到这样的路线:
this.router.navigate(['/pm/order', this.orderId]);