Angular 6路由器丢失路由参数

时间:2018-06-18 11:03:07

标签: angular angular-routing

我在使用Angular 6的路由时遇到了一些意想不到的行为。 给定一个主应用程序和延迟加载模块,我可以成功访问域/基本/名称,但访问路由参数的所有推荐方法都是空的。

//  Main routing module
const routes: Routes = [
  {
    path: '', component: LayoutComponent, children: [
      { path: 'base', loadChildren: 'baseModule' },
    ]
  }
];

----
----

//  Lazy loaded router module
const routes: Routes = [
  {
    path: '', component: BaseComponent, children: [
      { path: ':name', component: ChildComponent }
    ]
  }
];

----
----

  //    Base Component
  this.route.paramMap.subscribe(params => {
    this.foo = params.get('name');
    console.log(this.foo);
  });

鉴于这些代码块,this.foo 总是变为null。 我在某种程度上错误配置,或误解了这条路线?

1 个答案:

答案 0 :(得分:1)

您的参数路由,即{ path: ':name', component: ChildComponent }, 将打开子组件,因此,访问route参数的代码应该在ChildComponent而不是BaseComponent。