Angular路由和放置模块问题

时间:2018-10-26 11:40:58

标签: angular

给出以下路由配置:

以下模块已导入AppModule

const routes: Routes = [
 ...
  {
    path: "lazy",
    loadChildren: "app/modules/lazymodule#LazyModule"
  },
  {
    path: "**",
    component: DemoComponent,
    pathMatch: "full"
  }
]

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

以下模块已导入LazyModule

const routes: Routes = [
  {
    path: "",
    component: LazyComponent,
    pathMatch: "full"
  },
  {
    path: ":test",
    component: TestComponent
  },
  {
    path: "try",
    component: TryComponent,
    pathMatch: "full",

  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)]
})

一旦LazyModule已加载,我可以通过以下方式导航到TestComponent

this.router.navigate(["/lazy", "hello"])

但不通过

进入TryComponent
this.router.navigate(["/lazy/try"])

最后一个怎么了?

1 个答案:

答案 0 :(得分:1)

/lazy/try与以下项匹配:

{
    path: ":test",
    component: TestComponent
}

因此,应使用TestComponent作为try参数的值导航到:test

如果要更改此设置,请更改路由顺序,以使具有path: "try"的路由位于具有path: ":test"的路由之前。