Rafael Munitić
Rafael Munitić

Reputation: 927

Nest.js get injector instance

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 ?

Upvotes: 18

Views: 13658

Answers (1)

Alexey Petushkov
Alexey Petushkov

Reputation: 2168

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);
  }
}

Upvotes: 23

Related Questions