首页 > 解决方案 > 使用一个模块的角度多个延迟加载的路由/路径

问题描述

我正在尝试配置几条水平路线(即 - 不嵌套)并将它们放入一个延迟加载的模块中。

问题是,我无法弄清楚如何在延迟加载的功能模块本身中匹配这些路由。

这是主路由模块的样子 -

      {
        path: 'search/:query',
        loadChildren: () => import('src/pages/archives.module').then(m => m.ArchivesModule),
      },
      {
        path: 'category/:category',
        loadChildren: () => import('src/pages/archives.module').then(m => m.ArchivesModule),
      },
      {
        path: 'tag/:tag',
        loadChildren: () => import('src/pages/archives.module').then(m => m.ArchivesModule),
      },

然后,在我的功能模块中,我试图根据路径匹配这些路由,但它不起作用 -

      {
        path: 'search/:query',
        component: SearchComponent,
        resolver: SearchResolver,
      },
      {
        path: 'category/:category',
        component: CategoryComponent,
        resolver: CategoryResolver,
      },
      {
        path: 'tag/:tag',
        component: TagComponent,
        resolver: TagResolver,
      },

当我尝试完全访问重复的路由时,它确实有效,例如:

domain.com/category/:cat/category/:cat

所以我想唯一缺少的是一种在功能模块中匹配父路由的方法,但我在它周围找不到任何东西。

标签: angular

解决方案


The code structure is not clean. You are making a module that routes to another module using three routes. Why not removing the routes from the parent module, and just make them in the feature module. Parent Module:

{
    path: '/',
    loadChildren: () => import('src/pages/archives.module').then(m => m.ArchivesModule),
  }
  

Feature Module:

{
    path: 'search/:query',
    component: SearchComponent,
    resolver: SearchResolver,
  },
  {
    path: 'category/:category',
    component: CategoryComponent,
    resolver: CategoryResolver,
  },
  {
    path: 'tag/:tag',
    component: TagComponent,
    resolver: TagResolver,
  },

推荐阅读