当 nestjs 服务属于同一模块时,如何将其注入另一个服务?
How to inject a nestjs service into another service when both belong to the same module?
我在 NestJS 中有以下场景:
// user.service.ts
@Injectable()
export class UserService {
constructor(
private readonly userRepository: UserRepository,
private readonly userProfileService: UserProfileService,
) {}
}
// user-profile.service.ts
@Injectable()
export class UserProfileService {
constructor(
private readonly userProfileRepository: UserProfileRepository,
) {}
}
// user.module.ts
@Module({
imports: [DataRepositoryModule], // Required for the repository dependencies
providers: [UserService, UserProfileService],
exports: [UserService, UserProfileService],
})
export class UserModule {}
但是,当我尝试在另一个模块的控制器中使用 UserService 时,出现以下错误:
Nest can't resolve dependencies of the UserService (UserRepository, ?). Please make sure that the argument dependency at index [1] is available in the UserModule context.
Potential solutions:
- If dependency is a provider, is it part of the current UserModule?
- If dependency is exported from a separate @Module, is that module imported within UserModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
控制器代码:
@Controller()
export class UserController {
constructor(private readonly userService: UserService) {}
}
控制器模块:
@Module({
imports: [],
providers: [],
controllers: [UserController],
})
export class UserManagementModule {}
主要app.module.ts:
@Module({
imports: [
UserManagementModule,
UserModule,
DataRepositoryModule.forRoot(),
],
controllers: [],
providers: [],
})
export class AppModule {}
我很困惑,因为我完全按照错误提示执行操作,将这两个服务都添加到提供者数组 (UserModule) 中。我可能缺少什么?
从您的错误来看,您的文件导入之间似乎存在循环依赖关系。检查以确保您没有循环导入链(即 ServiceA 导入 ServiceB 导入 ServiceA 或 ServiceA 导入 ModuleA 导入 ServiceB 导入 ServiceA)。这在同一模块内使用桶文件时尤其常见。
我在 NestJS 中有以下场景:
// user.service.ts
@Injectable()
export class UserService {
constructor(
private readonly userRepository: UserRepository,
private readonly userProfileService: UserProfileService,
) {}
}
// user-profile.service.ts
@Injectable()
export class UserProfileService {
constructor(
private readonly userProfileRepository: UserProfileRepository,
) {}
}
// user.module.ts
@Module({
imports: [DataRepositoryModule], // Required for the repository dependencies
providers: [UserService, UserProfileService],
exports: [UserService, UserProfileService],
})
export class UserModule {}
但是,当我尝试在另一个模块的控制器中使用 UserService 时,出现以下错误:
Nest can't resolve dependencies of the UserService (UserRepository, ?). Please make sure that the argument dependency at index [1] is available in the UserModule context.
Potential solutions:
- If dependency is a provider, is it part of the current UserModule?
- If dependency is exported from a separate @Module, is that module imported within UserModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
控制器代码:
@Controller()
export class UserController {
constructor(private readonly userService: UserService) {}
}
控制器模块:
@Module({
imports: [],
providers: [],
controllers: [UserController],
})
export class UserManagementModule {}
主要app.module.ts:
@Module({
imports: [
UserManagementModule,
UserModule,
DataRepositoryModule.forRoot(),
],
controllers: [],
providers: [],
})
export class AppModule {}
我很困惑,因为我完全按照错误提示执行操作,将这两个服务都添加到提供者数组 (UserModule) 中。我可能缺少什么?
从您的错误来看,您的文件导入之间似乎存在循环依赖关系。检查以确保您没有循环导入链(即 ServiceA 导入 ServiceB 导入 ServiceA 或 ServiceA 导入 ModuleA 导入 ServiceB 导入 ServiceA)。这在同一模块内使用桶文件时尤其常见。