子路由中父路由的角度获取参数仅适用于具有空路径的参数



我有一个奇怪的问题,我有以下路由设置:

const routes: Routes = [
{ path: '', component: ANiceComponent, children: [
{ path: 'someurl/:id', component: AnOtherComponent, canActivate: [UserSignedInGuard], children: [
{ path: 'something', component: ASubComponent, pathMatch: 'full' },
{ path: '', component: AMainSubPageComponent, pathMatch: 'full' }
]}
]}
];

AMainSubPageComponent中,我可以访问id参数。在ASubComponent我无法访问id参数。我尝试将路径'something'更改为'',反之亦然。有了这个,我现在知道空路径有效,但非空路径不起作用。

我以相同的方式访问路线,并确保我没有丢失任何东西并以相同的方式进行。测试(我更改了测试路径(也表明这是由非空路径引起的。这是我访问参数的方式:

this.routeSub = this.route.paramMap.pipe(
map(params => {
console.log(params);
})
).subscribe();

我错过了什么吗? 对不起,我的英语不好..

您可以引用活动路由的父路由以访问父参数。

this.routeSub = this.route.parent.paramMap.pipe(
map(params => {
console.log(params);
})
).subscribe();

最新更新