'Angular getting data from one library and pass it another library

I have a library let's call it 'LibraryA', this library gets data from an angular app using forRoot config, and imports another library 'LibraryB', which also have forRoot config.

I would like pass the config from the app to LibraryA, and from LibraryA to LibraryB.

The first step works well :

interface LibAConfig {
   config1: string;
   config2: string;
}
export class LibraryAModule {
  static forRoot(configA: LibAConfig): ModuleWithProviders<LibraryAModule> {
    return {
      ngModule: LibraryAModule,
      providers: [{ provide: CONFIG, useValue: configA}],
    };
  }
}
@NgModule({
...
  imports: [
    LibraryAModule.forRoot(config),
  ],
...
})

but I couldn't make the second step work.

interface LibBConfig {
   config1: string;
}
export class LibraryBModule {
  static forRoot(configB: LibBConfig): ModuleWithProviders<LibraryBModule> {
    return {
      ngModule: LibraryBModule,
      providers: [{ provide: CONFIG, useValue: configB}],
    };
  }
}
@NgModule({
...
  imports: [
    LibraryBModule.forRoot(/*  I want to pass {config1: configA.config1} here  */),
  ],
...
})

I tried injecting the token in the constructor of the module class

let config: LibBConfig;

@NgModule({
...
  imports: [
    LibraryBModule.forRoot(config),
  ],
...
})
export class LibraryAModule {
  constructor(@Inject(CONFIG) config: LibAConfig) {
    config = {
      config1: config.config1,
    };
  }
}

but it's always undefined.

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source