Home > Enterprise >  Error: MyCustomHttpApiModule is already loaded. Import in your base AppModule only
Error: MyCustomHttpApiModule is already loaded. Import in your base AppModule only

Time:08-15

I'm new in angular and I was wondering if there is the possibility to load imported modules based on the condition from which module it was imported from.

I try to import MyCustomHttpApiModule from two difference modules, each time with a different config.

in app.module.ts:

    
@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    MyCustomHttpApiModule.forRoot(() => new CustomConfiguration({
      basePath: backendBaseUrl
    })),
    ]
    ...

and in a another.module.ts:

    
@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    MyCustomHttpApiModule.forRoot(() => new CustomConfiguration({
      basePath: `${backendBaseUrl}/suffix`
    })),
    ]
    ...

knowing that both modules are on the same app but are not related to each other.

I get the error:

Error: MyCustomHttpApiModule is already loaded. Import in your base AppModule only.

how can I import MyCustomHttpApiModule in base AppModule only, but each time with a different config? Thanks a lot for your help

CodePudding user response:

in your custom MyCustomHttpApiModule create two method forRoot and forChild

static forRoot() {
  return {
    ngModule: _,
    providers: []
}
static forChild() {
  return {
    ngModule: _,
    providers: []
}

and call forRoot with main module and forChild with anther module

  • Related