[英]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.