gpt4 book ai didi

dependency-injection - 在 NestJS 中注入(inject)父模块的服务

转载 作者:行者123 更新时间:2023-12-05 06:19:12 25 4
gpt4 key购买 nike

我在 NestJS 中有三个模块:EndpointModuleJWTModule 和作为模块的特定端点(例如 InfoModule)

我的 EndpointModule 看起来像这样:

@Module({
imports: [
JWTModule.withRSAKeys(
Path.resolveByApp('./private.key'),
Path.resolveByApp('./public.key')
),
InfoModule,
//More Endpoints
],
exports: [JWTModule]
})
export class EndpointModule {}

JWTModule 是这样的:

@Module({
providers: [JWTService],
exports: [JWTService]
})
export class JWTModule {
static async withRSAKeys(
privateKeyPath: string,
publicKeyPath: string
): Promise<DynamicModule> {
return {
module: JWTModule,
providers: await this.createProviders(privateKeyPath, publicKeyPath)
};
}

如您所见,JWTModule 是一个动态模块。现在我想在我的端点 Controller 中注入(inject)导出的 JWTService。例如:

@Module({
controllers: [InfoController]
})
export class InfoModule {}

@Controller()
export class InfoController {
constructor(private jwt: JWTService){};

这是行不通的。我必须在我的 InfoModule 中导入 EndpointModule,但这会产生循环依赖。有什么办法可以避免这种情况吗?我应该重新订购我的模块吗?

最佳答案

需要在InfoModule中引入JWTModule或者用Global()修饰EndpointModule

关于dependency-injection - 在 NestJS 中注入(inject)父模块的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60899528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com