简体   繁体   English

Angular 子模块中的多条路由

[英]Angular multiple routes in child module

If I have a Angular module, with these routes如果我有一个 Angular 模块,这些路线

const routes: Routes = [
  {path: 'route1', children: [
    {path: '', component: route1Component}
  ]},
  {path: 'route2', children: [
    {path: '', component: route2Component}
  ]},
  {path: 'route3', children: [
    {path: '', component: route3Component}
  ]}
];

how can I then use the routes in a parent module, where I would lazy load.然后我如何使用父模块中的路由,我会在其中延迟加载。 Normally I would do something like this通常我会做这样的事情

{path: 'route1', loadChildren: () => import('../child.module').then(m => m.ChildModule)}

but that normaly implies that the child route has a default route, and here I have 3但这通常意味着子路由有默认路由,这里我有 3

I would like to just import the module, and get all the routes from that module我只想导入模块,并从该模块获取所有路由

you have to provide any one route as default to the router in lazy loaded.您必须为延迟加载的路由器提供任何一个默认路由。 So I would suggest to just define the default router and redirect it to the first route.所以我建议只定义默认路由器并将其重定向到第一条路由。

So you can have the following code:所以你可以有以下代码:

const routes: Routes = [
 {path:''',redirectTo:'route1',pathMatch:'full'},
  {path: 'route1', children: [
    {path: '', component: route1Component}
  ]},
  {path: 'route2', children: [
    {path: '', component: route2Component}
  ]},
  {path: 'route3', children: [
    {path: '', component: route3Component}
  ]}
];

This would resolve your issue of not defining any default router.这将解决您未定义任何默认路由器的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM