Nest.js获取注入器实例 [英] Nest.js get injector instance
本文介绍了Nest.js获取注入器实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个通过Nest.js依赖项注入服务动态加载的类的实例.
I want to create an instance of a dynamically loaded class trough Nest.js dependency injection service.
在Angular中,我将使用Injector.create
,Nest.js中的等效值是什么?
In Angular I would use Injector.create
, what would be the equivalent in Nest.js ?
推荐答案
首先,您应该获得引用当前模块的ModuleRef,然后使用其"get"方法获取实例.
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);
}
}
这篇关于Nest.js获取注入器实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文