简体   繁体   English

Angular:延迟加载具有相同路由的多个模块

[英]Angular : Lazy load multiple Modules with the same route

I have an app where i want to lazy load two modules in the same moment with the same route path .我有一个应用程序,我想在同一时刻使用相同的路由路径延迟加载两个模块

My routing module would look like this :我的路由模块如下所示:

  {
    path: 'mypath',
    loadChildren: () => HomeModule,
    canLoad: [HomeGuard]
  },

   {
     path: 'mypath',
     loadChildren: () => AdvisorModule,
     canLoad: [AdvisorGuard]
   },

but this lead to load only the first one但这导致只加载第一个

i cant' find anyway to do it like this for example :我无论如何都找不到这样做,例如:

  {
    path: 'mypath',
    loadChildren: () => HomeModule, advisor module // ??
    canLoad: [// ??]
  },

I don't want also to import one of them in the other , as like this , only one module would be lazy loaded and the other automatically我也不想在另一个中导入其中一个,就像这样,只有一个模块会被延迟加载,而另一个会自动加载

How may it do it ??怎么可能呢??

You need to re-arrange your routes by one level and you also need to add auxiliary routes for the extra components you want to load.您需要将路线重新排列一级,还需要为要加载的额外组件添加辅助路线。

This works with Angular 9 (probably with 8 too)这适用于 Angular 9(也可能适用于 8)

{
  path: 'home',
  component: HostingComponentWithOutlets,
  children: [
    {
      path: 'featureOne',
      loadChildren: () => import('xxxxx').then(m => m.FeatureOneModule),
      canLoad: [featureOneGuard]
    },
    {
      path: 'featureTwo',
      outlet: 'outletAux1',
      loadChildren: () => import('yyyyy').then(m => m.FeatureTwoModule),
      canLoad: [featureTwoGuard]
    },
    // you can even load more components to different outlets from the same module
    // and update redirectTo and routerLink accordingly
    //{
    //  path: 'featureThree',
    //  outlet: 'outletAux2',
    //  loadChildren: () => import('yyyyy').then(m => m.featureTwoModule),
    //  canLoad: [featureTwoGuard]
    //},
    {
      path: '',
      redirectTo:
        '/absolute/path/to/home(featureOne/path-to-featureOne-component//outletAux1:featureTwo/path-to-featureTwo-component)',
      pathMatch: 'full'
    }
  ]
},
{ path: '', redirectTo: 'home', pathMatch: 'full' }

Hitting the 'home' route will lazy load all required modules.点击“home”路线将延迟加载所有必需的模块。

In your HostingComponentWithOutlets html template where you need to link to 'featureOne':在您需要链接到“featureOne”的HostingComponentWithOutlets html 模板中:

<a [routerLink]="featureOne/path-to-featureOne-component"   

and if you want to go straight to the full route with the auxiliary routes from a template:如果您想使用模板中的辅助路线直接进入完整路线:

<a [routerLink]="['.', { outlets: { 'primary': ['featureOne', 'path-to-featureOne-component'], 'outletAux1': ['featureTwo', 'path-to-featureTwo-component'] } }]"   

FeatureOneModule should define 'path-to-featureOne-component' and FeatureTwoModule should define 'path-to-featureTwo-component' in their equivalent route definitions. FeatureOneModule应定义“path-to-featureOne-component”,而FeatureTwoModule应在其等效路由定义中定义“path-to-featureTwo-component”。

You could rework things like this:你可以像这样返工:

const routes: Routes = [
  {
    path: 'mypath/home',
    loadChildren: () => HomeModule,
    canLoad: [HomeGuard]
  },
  {
    path: 'mypath/advisor',
    loadChildren: () => AdvisorModule,
    canLoad: [AdvisorGuard]
  },
]

In other words move the route path to your module outside to the parent module, in this case I assume those are 'adviser' and 'home'换句话说,将模块的路径路径移到父模块之外,在这种情况下,我假设它们是“顾问”和“家”

And then just start in the module routing with a redirect solution and/or a path like so:然后使用重定向解决方案和/或像这样的路径在模块路由中开始:

Home module routing:家庭模块路由:

const routes: Routes = [
  {
    path: '', // <-- in your current solution probably 'home'
    component: HomeParentComponent,
    children: [
      { path: '', redirectTo: 'childOne', pathMatch: 'full' },
      { path: 'childOne', component: HomeChildComponentOne },
    ],
  },
];

Advisor module routing:顾问模块路由:

const routes: Routes = [
  {
    path: '', // <-- in your current solution probably 'advisor'
    component: AdvisorParentComponent,
    children: [
      { path: '', redirectTo: 'childOne', pathMatch: 'full' },
      { path: 'childOne', component: AdvisorChildComponentOne },
    ],
  },
];

This works nicely, you should be able to navigate to:这很好用,您应该能够导航到:

'/mypath/home' and end up inside your HomeParentComponent with router outlet of HomeChildComponent one. '/mypath/home'并以HomeParentComponent的路由器出口结束在您的HomeChildComponent

'/mypath/advisor' and end up inside your AdvisorParentComponent with router outlet of AdvisorChildComponent one. '/mypath/advisor'并最终在您的AdvisorParentComponent使用AdvisorChildComponent路由器出口之一。

In case you don't want a child component inside your router outlet it is even simpler, you can just remove the child routes and redirect.如果您不希望路由器插座内有子组件,那就更简单了,您可以删除子路由并重定向。


Note: If this solution doesn't resolve your issue, then please share more details on your module routing so I can get a better picture of your desired route configuration.注意:如果此解决方案不能解决您的问题,请分享有关您的模块路由的更多详细信息,以便我可以更好地了解您所需的路由配置。

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

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