2

我们有以下路由场景:

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

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

4

2 回答 2

7

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

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

接着

RouterModule.forRoot(routes,routingConfiguration),
于 2018-08-13T07:34:33.787 回答
0

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

从子组件访问父参数:

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

this._route在哪里private _route: ActivatedRoute

于 2018-08-13T07:33:45.227 回答