路由器存储无法识别路由器参数



我们有以下路由方案:

export const ROUTES: Routes = [
{
path: ':userId',
component: fromContainers.OrganizationComponent,
canActivate: [],
children: [
{ path: '', pathMatch: 'full', redirectTo: 'posts' },
{
path: 'posts',
loadChildren: './views/posts/posts.module#PostsModule'
}
]
}
];

我们的路由器存储无法以这种方式识别userId但是当我们删除children属性时,它确实可以识别userId的值 .

http://localhost:4200/user/r1RORssFG --> WORKS
http://localhost:4200/user/r1RORssFG/posts --> DOES NOT WORK

这种行为背后的原因是什么?

根据Jota.Toledo的提示,解决方案是定义一个路由配置,我在其中使用如下Router.forRoot()

export const routingConfiguration: ExtraOptions = {
paramsInheritanceStrategy: 'always'
};

然后

RouterModule.forRoot(routes,routingConfiguration),

我认为您无法从子路由访问父路由的参数。

要从子组件访问父参数,请执行以下操作:

使用this._route.parent.params而不是this._route.params

this._route在哪里private _route: ActivatedRoute

最新更新