简体   繁体   English

Angular 8-如何重写BrowserModule以进行多应用程序延迟加载?

[英]Angular 8 - How to override BrowserModule for multi-apps lazy loading?

Having tested successfully a sub-app routing, I called then the same sub-app from its parent app, but this time the routing doesn't work and the following console error is shown: 成功测试了子应用程序路由后,我从其父应用程序中调用了相同的子应用程序,但是这次路由不起作用,并显示以下控制台错误:

Error: BrowserModule has already been loaded. 错误:已加载BrowserModule。 If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import CommonModule instead. 如果您需要从延迟加载的模块访问诸如NgIf和NgFor之类的通用指令,请改为导入CommonModule。

With the following code I am trying to override the sub-app BrowserModule with CommonModule that should be exported to the parent app, because BrowserModule should be used only once, and in fact, it is already used by the parent app.module.ts . 使用以下代码,我尝试使用应导出到父应用程序的CommonModule覆盖子应用程序BrowserModule,因为BrowserModule仅应使用一次,并且实际上,它已由父app.module.ts So my sub-app app.module.ts is as follows: 所以我的子应用程序app.module.ts如下:

    const routes: Routes = [
      {
        path: 'module1',
        loadChildren: () => import('../../../sub-app1/src/app/module1/module1.module').then( module => module.Module1Module )
      },
      {
        path: 'module2',
        loadChildren: () => import('../../../sub-app1/src/app/module2/module2.module').then( module => module.Module2Module )
      }
    ];

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        RouterModule.forRoot(routes)
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

    @NgModule({
      imports: [
        AppModule,
        CommonModule,
        RouterModule.forChild(routes),
      ]
    })
    export class SubApp1Module {}

So what can be done to get the routing working from the parent app? 那么,如何做才能从父应用程序开始路由呢?

You shouldn't be importing AppModule from SubApp1Module. 您不应该从SubApp1Module导入AppModule。

  • Remove that "AppModule" import and you should be OK. 删除该“ AppModule”导入,您应该就可以了。
  • If there is something in AppModule that you would like to to share with other modules, create a "SharedModule" and import that one instead. 如果您想在AppModule中与其他模块共享某些内容,请创建一个“ SharedModule”,然后导入该模块。

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

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