Angular Router:将模块作为延迟加载模块的子级加载

时间:2019-07-05 06:39:35

标签: angular lazy-loading angular-routing angular-router angular-module

我有一个复杂的应用程序,我想在其中加载模块作为延迟加载模块的子模块。

例如,我想要以下路径:

https://localhost:8080/ui/ 示例模块 /

examplemodule new 都是一个模块,每个模块都有自己的routing.config文件。

我的app-routing.module.ts看起来像这样:

const routes: Routes = [
  {
    path: '',
    component: ParentComponent,
    canActivate: [LoginRequiredGuard],
    children: [
      {
        path: '',
        children: [
          {
            path: '',
            component: HomeComponent,
          },
          {
            path: 'examplemodule',
            loadChildren: 'app/my-modules/example/example.module#ExampleModule',
            canActivate: [LoginRequiredGuard],
          },
          {
            // examplemodule2
          },
          {
            // examplemodule3
          },
          {
            path: 'new',
            loadChildren: 'app/new/new.module#NewModule',
            canActivate: [LoginRequiredGuard],
          },
        ],
      },
      ...

NewModule的new.routing.ts文件如下所示:

const routes: Routes = [
  {
    path: '',
    redirectTo: 'new',
    pathMatch: 'full'
  },
  {
    path: 'new',
    component: NewViewComponent,
  },
];

正如我目前所做的那样,我得到了“找不到资源”。

例如,我不要以下路线:

https://localhost:8080/ui/ 示例模块 /

https://localhost:8080/ui/ examplemodule2 /

https://localhost:8080/ui/ 示例模块3 /

我在做什么错?我希望我可以理解地解释它。

1 个答案:

答案 0 :(得分:0)

我认为您需要从NewModule内部加载ExampleModule

因此,从应用模块路由中删除您的new路径,并将此位添加到ExampleModule的路由中

const routes: Routes = [
{
    path: '',
    component: YourParentComponentForExampleModule,
    children: [
      {
        path: '',
        children: [
          //All your other paths here
          //...
          {
            path: 'new',
            loadChildren: 'app/new/new.module#NewModule',
            //canActivate: [LoginRequiredGuard], //You probably don't need it as it's already there on parent module
          },
        ],
      },
  ...