【问题标题】:Add Route Dynamically To Lazy Loaded Module Angular 8将路由动态添加到延迟加载的模块Angular 8
【发布时间】:2019-10-30 19:19:33
【问题描述】:

我有一个共享模块,其路由定义如下

export const HrRouting: Routes = [
  {
    path: '',
    component: HrComponent,
    data: {
      breadcrumb: 'HR'
    },
    children: [
      {
        path: 'settings',
        component: SalesOneProHrSettingsComponent,
        data: {
          acl: 'hr::hr',
          breadcrumb: 'Settings'
        },
      }
    ]
  }
]

我正在使用 loadChildren 将这个模块加载到两个不同的模块中。在其中一个模块中,我想为这条路线再添加一条路径。例如

HrRouting['children'].push({
        path: 'reports',
        component: HrReportComponent
});

我怎样才能做到这一点?

【问题讨论】:

  • 你找到解决方案了吗??我正在寻找相同的 .如果您有解决方案,请在此处发布。

标签: angular angular8


【解决方案1】:

您可以使用与上面相同的模式。

{
path:'',
component: something,
children:
   [
     path: 'settings',
     component: settings,
     children: 
        [
           path: 'nested',
           component: nested
        ]
   ]
}

【讨论】:

    【解决方案2】:

    你可以使用 unshift 来代替 Push

    this.router.config.unshift(
      { path: 'page1', component: Page1Component },
      { path: 'page2', component: Page2Component }
    );
    

    在使用推送时,路由正在更新到最后,在path:'**'之后,因此这些路由将无法到达。

    请查看链接了解更多信息 https://medium.com/@DenysVuika/dynamic-routes-with-angular-6fda03b7fa2c

    【讨论】:

    • 我正在使用 loadChildren 添加模块,所以 this.router.config.unshift 不起作用
    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2017-08-21
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多