简体   繁体   English

延迟加载的多级模块不起作用

[英]multi level module with lazy loading is not working

I have this multi level module with each routing module, but only the first routing module is working, the second module is not. 我的每个路由模块都具有这个多级模块,但是只有第一个路由模块正在工作,而第二个模块却没有。

Each module is lazy loading, with default path is aim to the first module. 每个模块都是延迟加载的,默认路径指向第一个模块。

app.module app.module

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    CoreModule,
    SharedModule,
    FeaturesModule,

    RouterModule.forRoot([
      { path: '', loadChildren: './features/features.module#FeaturesModule'}
    ]),
  ],
  providers: [],
  bootstrap: [AppComponent]
})

features.module features.module

@NgModule({
  imports: [
    CoreModule,
    FeaturesRoutingModule,
    MainModule,
    RegisterModule
  ]
})

//routing
const routes: Routes = [
 { path: '', loadChildren: './main/main.module#MainModule' },
 { path: NAVIGATIONS.REGISTER, loadChildren: './register/register.module#RegisterModule' },
 ];

main.module main.module

@NgModule({
  imports: [
    CoreModule,
    SharedModule,
    MainRoutingModule,
    MainFeaturesModule
  ],
 /..../})

// routing
const routes: Routes = [
  {
    path: '', component: MainPageComponent,
    children: [
      { path: '', component: LandingPageComponent },
      /..../
      { path: NAVIGATIONS.EXPLORE, loadChildren: './main-features/explore/explore.module#ExploreModule'},
      { path: '**', component: NotfoundPageComponent }
    ]
  }
];

register.module register.module

@NgModule({
  imports: [
    CoreModule,
    SharedModule,
    RegisterRoutingModule
  ],
  /.../})

// routing
const routes: Routes = [
  { path: '', component: RegisterPageComponent }
];

Hierarchy 等级制度

app.module
  |
  L features.module // lazy load default
       |
       L main.module // lazy load default
       |
       L register.module

the main module routing is working just fine, but the register module is not, there is no error or anything, but anything I put in the register module routing AND the features module routing is not working and always get to 'Page Not Found' component which I declared in main module routing. 主模块路由工作正常,但注册模块不工作,没有错误或其他任何东西,但是我在注册模块路由中输入的任何内容以及功能模块路由均无法正常工作,并始终进入“找不到页面”组件我在主模块路由中声明的。

the reason why I put main module and register module separate is because the main module have this header and footer which I don't want to show in register, then the 'Page Not Found' can get the header and footer. 之所以将主模块和寄存器模块分开,是因为主模块具有不想在寄存器中显示的页眉和页脚,因此“找不到页面”可以获得页眉和页脚。

how to tackle this issue with still having multi level module routing? 如何通过仍然具有多级模块路由来解决此问题?

When you want to lazy load some modules in your angular applications, NEVER EVER import them anywhere. 当您想在角度应用程序中延迟加载某些模块时,切勿将它们导入任何地方。

In your app module, you are importing FeaturesModule which breaks the lazy loading. 在您的应用程序模块中,您正在导入FeaturesModule ,这会打破延迟加载。 So, delete FeaturesModule import from app.module.ts Likewise, you should delete MainModule , RegisterModule from features.module.ts 因此,从app.module.ts删除FeaturesModule导入同样,您也应该从features.module.ts删除MainModuleRegisterModule

What this does is that redefine path: '' multiple times. 这就是重新定义path: ''多次。

I, also, noticed that you are importing CoreModule in lazy loaded modules. 我也注意到,您正在延迟加载的模块中导入CoreModule I assume your CoreModule provide some services. 我假设您的CoreModule提供了一些服务。 Importing a module, which provides services, in a lazy loaded module will result in different instances of services. 在延迟加载的模块中导入提供服务的模块会导致不同的服务实例。 Lazy loaded modules have their own dependency injection mechanism. 延迟加载的模块具有其自己的依赖项注入机制。

For more information check the docs 有关更多信息, 请检查文档

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

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