【问题标题】:Angular multiple routes in child module子模块中的角度多条路线
【发布时间】:2020-08-12 13:14:22
【问题描述】:

如果我有一个 Angular 模块,使用这些路由

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

然后我怎样才能使用父模块中的路由,我会在其中延迟加载。 通常我会做这样的事情

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

但这通常意味着子路由有一个默认路由,这里我有 3 个

我只想导入模块,并从该模块获取所有路由

【问题讨论】:

    标签: angular routes


    【解决方案1】:

    你必须为延迟加载的路由器提供任何一个默认路由。 所以我建议只定义默认路由器并将其重定向到第一个路由。

    所以你可以有以下代码:

    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}
      ]}
    ];
    

    这将解决您未定义任何默认路由器的问题。

    【讨论】:

    • 谢谢,但这并不能完全解决我的问题。然后如何从父级路由到“route2”和“route3”?
    • 然后你必须在 route1 中放一些链接来重定向到 route2 和 route3。如果你不想这么说,那就没有办法将它们作为一个模块使用并保持直接链接,那么最好制作三个子模块。您在与基本概念相冲突。
    • 谢谢。是的,这是我第一次遇到这个问题。我只是将组件添加到父模块中,即使它并不理想
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2015-02-14
    相关资源
    最近更新 更多