Nest.js get injector instance

2019-03-20 15:34发布

问题:

I want to create an instance of a dynamically loaded class trough Nest.js dependency injection service.

In Angular I would use Injector.create, what would be the equivalent in Nest.js ?

回答1:

First of all you should get a ModuleRef which references current module, and then use its "get" method to get an instance.

@Injectable()
export class AppletService {
  files: FileService;

  constructor(
    private moduleRef: ModuleRef,
  ) { 
    this.files = moduleRef.get(FileService);
  }
}