Angular: Provide Injection token to a module imported in a lazy loaded module

Viewed 919

I am having issues in providing injection tokens to a module that I import in a lazy loaded module. In the module, I defined a withConfig static function to set configuration:

  static withConfig(
    config: Config
  ): ModuleWithProviders<MySubModule> {
    return {
      ngModule: MySubModule,
      providers: [
        {
          provide: SUBS_MANAGER,
          useExisting: config.subsManager,
        },
        {
          provide: QUERY_SERVICE,
          useExisting: config.dataApi,
        },
      ],
    };
  }

subsManagerand dataApi are both services:

export const QUERY_SERVICE = new InjectionToken<Type<any>>('QueryService');
export const SUBS_MANAGER = new InjectionToken<SubscriptionManager>(
  'SubscriptionManagerService'
);

This is what I do in my lazy-loaded module:

@NgModule({
  imports: [
    //all my other modules
    MySubModule.withConfig({
      dataApi: APIService,
      subsManager: SubscriptionManagerService,
    }),
   providers: [
     APIService,
     SubscriptionManagerService,
  ],

But I get this error:

R3InjectorError(AppModule)[InjectionToken SubscriptionManagerService -> InjectionToken SubscriptionManagerService -> InjectionToken SubscriptionManagerService]: 
      NullInjectorError: No provider for InjectionToken SubscriptionManagerService!

How can I correctly set tokens for this module?

0 Answers
Related