【发布时间】:2020-07-08 23:46:41
【问题描述】:
我在 NestJS 中有三个模块:EndpointModule、JWTModule 和作为模块的特定端点(例如 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 是一个动态模块。现在我想在我的端点控制器中注入导出的JWTService。例如:
@Module({
controllers: [InfoController]
})
export class InfoModule {}
@Controller()
export class InfoController {
constructor(private jwt: JWTService){};
这不起作用。我必须在我的InfoModule 中导入EndpointModule,但这会产生循环依赖。有没有办法避免这种情况?我应该重新排序我的模块吗?
【问题讨论】:
-
您是否将 JWTModule 添加为 InfoModule 的导入?
-
我不能这样做,因为 JWTModule 是动态的并且需要参数(例如在 EndpointModule 中)@ChukwumaEzumezu 请在 Maciej 的回答中查看我的评论
标签: dependency-injection nestjs circular-dependency