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

时间:2018-08-12 13:08:19

标签: angular angular6 ngrx ngrx-store ngrx-router-store

我们有以下路由方案:

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

此行为背后的原因是什么?

2 个答案:

答案 0 :(得分:1)

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

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

然后

RouterModule.forRoot(routes,routingConfiguration),

答案 1 :(得分:0)

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

要从子组件访问父参数:

使用this._route.parent.params代替this._route.params

其中this._routeprivate _route: ActivatedRoute