#ModuleRef.create() does not inject providers

9 messages · Page 1 of 1 (latest)

signal hearth
#

Hello,

I have the following module:

@Module({
  providers: [
    MyStaticService,
    LoggingService
  ]
})
export class MyModule {}

I want to dynamically load in a class (MyDynamicService) at runtime, to do this I plan on using ModuleRef.

I have the following code:

class MyStaticService {
  constructor(private readonly moduleRef: ModuleRef) {}

  async doSomething() {
    await this.moduleRef.create(MyDynamicService);
  }
}

This works, however, MyDynamicService is instantiated without any dependencies.

@Injectable()
class MyDynamicService {
   constructor(logger: LoggingService) {
      logger.info('hello'); // TypeError: info is not property on undefined
   }
}

What am I doing wrong here? How can I get ModuleRef.create() to also handle injecting the logger?

Thanks!

limpid patio
#

What if you move that this.logger.info to the onModuleInit instead?

signal hearth
#

It never gets called (I assume you mean onModuleInit() on MyDynamicService)

limpid patio
#

Oh, that’s correct

signal hearth
#

What do you mean, sorry?

limpid patio
#

can you share a minimum reproduction of that code? I didn't see anything wrong

signal hearth
#

I figured it out, I had forgot to make something injectable

#

Thanks though!