[英]Dependency hell in NestJS/TS
我用许多模块构建NestJS
项目,最近我有点迷失了,我做的最后一件事是将QueueService
添加到我的ProjectService
和ProjectModule
中,但是在启动整个应用程序后,编译器向我抛出了这个:
Error: Nest can't resolve dependencies of the QueueService (UtilsService, UserService, Connection, ?). Please make sure that the argument Object at index [3] is available in the ProjectModule context.
QueueService
的 index[3] 处的参数是ProjectService
,那么为什么他们希望我将ProjectModule/ProjectService
导入我的ProjectModule
? :P
这是我的所有代码:
@Injectable()
export class ProjectService {
constructor(
private conn: Connection,
private utilsService: UtilsService,
private userService: UserService,
private notificationService: NotificationsService,
private queueService: QueueService
) { }
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt'})
],
providers: [ProjectService, UtilsService, UserService, NotificationsService, QueueService ],
controllers: [ProjectController],
exports: [ProjectService]
})
export class ProjectModule {}
@Injectable()
export class QueueService {
constructor(
readonly conn: Connection,
readonly utilsService: UtilsService,
readonly userService: UserService,
readonly projectService: ProjectService
){}
}
@Module({
imports: [
AuthModule,
PassportModule.register({ defaultStrategy: 'jwt'})],
providers: [QueueService , UtilsService, UserService, NotificationsService, ProjectService],
controllers: [QueueController ],
exports: [PassportModule]
})
export class QueueModule {}
应用模块
@Module({
imports: [
...,
TypeOrmModule.forRootAsync({
useClass: TypeOrmConfigService
}),
PassportModule.register({ defaultStrategy: 'jwt'}),
JwtModule.register({
secret: 'secretKey'
}),
ScheduleModule.forRoot(),
...,
...,
QueueModule,
...,
...,
ProjectModule,
...,
...,
...
],
controllers: [..., ..., ..., ..., ...],
providers: [ ..., ..., ..., ..., ...,ProjectService, ..., ..., QueueService],
})
export class AppModule {}
感谢您的帮助,我在这里停留了 3-4 小时,我不知道我还能做些什么:(
///////////////////////////////
好的,这里有很多东西要解压。 所以你的服务之间有循环依赖关系,你的模块之间应该有循环依赖关系。 现在发生的事情是您正在创建QueueService
的两个实例和ProjectService
的两个实例。 然而,由于这些服务相互依赖,Nest 没有办法正确地实例化它们(至少目前是这样)。
因此,如果您想要每个两个,这里快速简便的解决方法是为循环依赖的类添加正确的forwardRef
。
@Injectable()
export class QueueService {
constructor(
readonly conn: Connection,
readonly utilsService: UtilsService,
readonly userService: UserService,
@Inject(forwardRef(() => ProjectService))
readonly projectService: ProjectService
){}
}
@Injectable()
export class ProjectService {
constructor(
private conn: Connection,
private utilsService: UtilsService,
private userService: UserService,
private notificationService: NotificationsService,
@Inject(forwardref(() => QueueService))
private queueService: QueueService
) { }
现在,如果这就是您想要的,太好了,请随时停止阅读此处和 go 关于您的业务。
那么应该发生什么? 如果您想要每个服务的真正单例(服务只创建一次),您应该通过它们所属的模块共享提供者并导入模块,而不是创建新的provider
值。 由于您的ProjectModule
和QueueModule
是循环依赖的,因此您需要再次使用forwardRef
。 上面的服务仍然有效,所以我不用担心重写它们,但是你的模块应该如下所示:
@Module({
imports: [
AuthModule,
forwardRef(() => ProjectModule),
PassportModule.register({ defaultStrategy: 'jwt'})],
providers: [QueueService , UtilsService, UserService, NotificationsService],
controllers: [QueueController ],
exports: [PassportModule, QueueService]
})
export class QueueModule {}
@Module({
imports: [
forwardref(() => QueueModule),
PassportModule.register({ defaultStrategy: 'jwt'})
],
providers: [ProjectService, UtilsService, UserService, NotificationsService ],
controllers: [ProjectController],
exports: [ProjectService]
})
export class ProjectModule {}
您绝对应该查看您的UtilsModule
、 NotificationsModule
和UserModule
以清理它们的导出,并重新使用模块而不是新的提供程序实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.