4

I've created two Angular libraries, one has the other as a dependency.

the dependency needs to be configured using the forRoot method. how can i pass the configuration data from the parent library to it's dependency?

for example, let's say we have TopLevelLib, which has OtherLib as a dependency. OtherLib needs to be passed a configuration object using forRoot.

End user's AppModule, imports to

@NgModule({
  imports: [
    TopLevelLib.forRoot(someConfigData)
  ],
  declarations: [...],
  exports: [...]
})
export class AppModule { }

TopLevelLib - imported to AppModule by the end user

@NgModule({
  imports: [
    ...
    OtherLib.forRoot(*****what goes in here?*****)
  ],
  declarations: [...],
  exports: [...]
})
export class TopLevelLib {
  static forRoot(config: ConfigObj): ModuleWithProviders {
    return {
      ngModule: SampleModule,
      providers: [{ provide: SomeInjectionToken, useValue: config }]
    };
  }
}

OtherLib - imported by TopLevelLib

@NgModule({
  imports: [...],
  declarations: [...],
  exports: [...]
})
export class OtherLib {
  static forRoot(config: ConfigObj): ModuleWithProviders {
    return {
      ngModule: SampleModule,
      providers: [{ provide: SomeInjectionToken, useValue: config }]
    };
  }
}

What i need is to pass the configuration object instance from TopLevelLib to OtherLib. So that when the end user configures TopLevelLib using forRoot, OtherLib will be configured with the same data.

Any ideas on how to implement this?

Roip
  • 51
  • 5

2 Answers2

0

You can make your forRoot parameters typed. You have explicitly defined that OtherLib has config: ConfigObj as a parameter - that means that the TopLevelLib needs to configure it with an instance of ConfigObj. So the answer to *****what goes in here?***** comment is: an instance of ConfigObj.

Edit: after comments, you seem to want to pass-through some config value. You can do it this way:

export class TopLevelLib {
  static forRoot(config: ConfigObj): ModuleWithProviders {
    return {
      ngModule: SampleModule,
      providers: [{ provide: ConfigObj, useValue: config }]
    };
  }
}

Then the OtherLib can use Injector to get this:

class OtherLib {
  constructor(@Inject() ConfigObj) {}
...
Zlatko
  • 16,414
  • 12
  • 60
  • 116
  • what i need is to pass the configuration object instance from TopLevelLib to OtherLib. so that when the end user configures TopLevelLib, OtherLib will be configured with the same data. – Roip Dec 19 '18 at 14:00
  • i think that because OtherLib is imported to my library from an external source, i need to import it with the config value otherwise the injected object will not be available. – Roip Dec 19 '18 at 16:02
  • Well. If you're using the otherlib only in your toplevellib - then only toplevellib imports it and can provide the dependency needed. If you can provide it elsewhere too, and you need this config to be shared, then I guess this should be at the nearest common parent. If the config is per-module-instance, then yeah, something like forRoot. But then maybe do not add the module directly to toplevellib imports - rather in toplevellib.forroot(), return a different, dynamically created module, with otherlib configured as you please as an import. – Zlatko Dec 19 '18 at 21:44
0

Eventually, i found a good solution. I've exposed the injection token used by the service (in the example above, OtherLib), imported it to the TopLevelLib module, and provided it to the module instance using the forRoot configuration.

Roip
  • 51
  • 5