Nest.js get injector instance

怎甘沉沦 提交于 2019-12-04 12:18:58

问题


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


来源:https://stackoverflow.com/questions/49031159/nest-js-get-injector-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!