简体   繁体   English

NestJS ModuleRef 实例

[英]NestJS ModuleRef instances

I'm relatively new to nestJS and ran into a circular dependency with REQUEST Scoped services that got me reading about ModuleRef in nestJS.我是 nestJS 的新手,遇到了 REQUEST Scoped 服务的循环依赖,这让我阅读了 nestJS 中的 ModuleRef。

I was looking at the documentation where it mentions that resolving the provider by calling it consecutively returns multiple unique instances for this provider.我正在查看文档,其中提到通过连续调用它来解析提供程序会为此提供程序返回多个唯一实例。

Now for my use case I only need to call this once so this is not really relevant to me.现在对于我的用例,我只需要调用一次,所以这与我无关。 But I'd like to know when I may have to make these consecutive calls and why we'd need 2 instances of the same provider.但我想知道什么时候我可能必须进行这些连续调用以及为什么我们需要同一提供程序的 2 个实例。

But I'd like to know when I may have to make these consecutive calls and why we'd need 2 instances of the same provider.但我想知道什么时候我可能必须进行这些连续调用以及为什么我们需要同一提供程序的 2 个实例。

That's just the thing, generally you wouldn't need to have two instances, but the specific documentation you're looking at is regarding TRANSIENT scoped providers, which are different at every point the are injected, that is, each instance should be unique, which is exactly what the documentation is showing here.就是这样,通常您不需要有两个实例,但是您正在查看的特定文档是关于TRANSIENT范围提供程序的,它们在注入的每个点都是不同的,也就是说,每个实例都应该是唯一的,这正是文档在此处显示的内容。 It then goes to show that if you need the same instance, you should use a constant for the contextId passed to ModuleRef#resovle to ensure that you get the same instance that your resolved earlier然后它表明,如果您需要相同的实例,则应该为传递给ModuleRef#resovlecontextId使用常量,以确保您获得与之前解析的实例相同的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM