Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
APP_INITIALIZER for lazy loaded module

I am developing a module that suppport forRoot and forChild. Whenever this module is imported with forRoot, forChild some methods of a custom service should be called. It works well for forRoot since I use APP_INITIALIZER, but the factory is not called anymore in the forChild of a lazy loaded module. How can I execute the methods that I have in my factory in the forChild so that it also works with lazy loaded modules? Is there some sort of equivelant of the APP_INITIALIZER for lazy loaded modules or a workaround?

export const TRANSLATION_CONFIGURATION = new InjectionToken<TranslationConfiguration<any>>('');

function initializeAppFactory(config: TranslationConfiguration<any> & { language: Language }, locutus: LocutusService): () => void {
  return () => {
      if (config.language) {
        locutus.registerRoot(config);
        return;
      }

      locutus.registerChild({
        scope: config.scope,
        loaders: config.loaders
      });
  };
}

@NgModule({
  declarations: [
    NgxLocutusComponent,
    LocutusDirective,
    LocutussPipe
  ],
  imports: [
    CommonModule
  ],
  exports: [
    NgxLocutusComponent,
    LocutusDirective,
    LocutussPipe
  ]
})
export class LocutusModule {

  static forRoot<T>(config: TranslationConfiguration<T> & { language: Language }): ModuleWithProviders<LocutusModule> {
    return {
      ngModule: LocutusModule,
      providers: [
        {
          provide: TRANSLATION_CONFIGURATION,
          useValue: config,
        },
        {
          provide: APP_INITIALIZER,
          useFactory: initializeAppFactory,
          deps: [
            TRANSLATION_CONFIGURATION,
            LocutusService
          ],
          multi: true
        },
      ]
    };
  }

  static forChild<T>(config: TranslationConfiguration<T>): ModuleWithProviders<LocutusModule> {
    return {
      ngModule: LocutusModule,
      providers: [
        {
          provide: TRANSLATION_CONFIGURATION,
          useValue: config,
        },
        {
          provide: APP_INITIALIZER,
          useFactory: initializeAppFactory,
          deps: [
            TRANSLATION_CONFIGURATION,
            LocutusService
          ],
          multi: true
        },
      ]
    };
  }
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda