场景一
写法一:
export class InfrastructureStackService {
constructor(
private manager: InfrastructureStackManager = InfrastructureStackManager.PULUMI
) {}
}

写法二:
export class InfrastructureStackService {
private manager: InfrastructureStackManager =
InfrastructureStackManager.PULUMI;
constructor() {}
}

场景二
@Injectable()
export class SqsService {
private client: SQSClient;
private queueUrl: string;
constructor(params: {queueUrl: string}) {
this.client = new SQSClient({
region: AwsConfig.getRegion(), // region is required for SQS service.
});
this.queueUrl = params.queueUrl.toString();
}
}
[9:15:20 AM] Found 0 errors. Watching for file changes.
[Nest] 96503 - 09/12/2022, 9:15:25 AM LOG [NestFactory] Starting Nest application...
[Nest] 96503 - 09/12/2022, 9:15:25 AM LOG [InstanceLoader] PrismaModule dependencies initialized +82ms
[Nest] 96503 - 09/12/2022, 9:15:25 AM ERROR [ExceptionHandler] Nest can't resolve dependencies of the SqsService (?). Please make sure that the argument Object at index [0] is available in the AwsModule context.
Potential solutions:
- If Object is a provider, is it part of the current AwsModule?
- If Object is exported from a separate @Module, is that module imported within AwsModule?
@Module({
imports: [ /* the Module containing Object */ ]
})
这并不是 Nestjs 的 bug。
本文探讨了在NestJS框架中遇到的依赖注入问题,通过分析两个不同场景——场景一和场景二,揭示了问题的本质。作者指出,这些问题并非NestJS的bug,而是需要正确理解和使用自定义提供者。详细解决方案可参考NestJS官方文档,该文档详细介绍了如何在NestJS中创建和使用自定义提供者。
https://docs.nestjs.com/fundamentals/custom-providers
1156

被折叠的 条评论
为什么被折叠?



