Nestjs 为什么我会得到一个;巢罐';t解决依赖关系“;
我正在试图找出为什么会出现以下错误: Nest无法解析UniqueInteractionService(?)的依赖项。 请确保索引[0]处的参数依赖关系为 在SharedModule上下文中可用 我的班级: sharedModule.ts:Nestjs 为什么我会得到一个;巢罐';t解决依赖关系“;,nestjs,Nestjs,我正在试图找出为什么会出现以下错误: Nest无法解析UniqueInteractionService(?)的依赖项。 请确保索引[0]处的参数依赖关系为 在SharedModule上下文中可用 我的班级: sharedModule.ts: import {Module, Global} from '@nestjs/common'; import {InteractionsService} from "./elasticsearch/interactionsService"; import {U
import {Module, Global} from '@nestjs/common';
import {InteractionsService} from "./elasticsearch/interactionsService";
import {UniqueInteractionsService} from "./elasticsearch/uniqueInteractionsService";
import {EsProvider} from "./elasticsearch/esProvider";
@Global()
@Module({
exports: [InteractionsService, UniqueInteractionsService],
providers: [EsProvider, InteractionsService, UniqueInteractionsService]
})
export class SharedModule {
}
interacionsService.ts:
import {ESService} from "./ESService";
import {Injectable, Inject} from '@nestjs/common';
@Injectable()
export class InteractionsService{
constructor(@Inject(ESService) private readonly esService: ESService) {}
// more stuff
}
import {ESService} from "./ESService";
import {Injectable, Inject} from '@nestjs/common';
@Injectable()
export class UniqueInteractionsService{
constructor(@Inject(ESService) private readonly esService: ESService) {}
// more stuff
}
UniqueInteractionService.ts:
import {ESService} from "./ESService";
import {Injectable, Inject} from '@nestjs/common';
@Injectable()
export class InteractionsService{
constructor(@Inject(ESService) private readonly esService: ESService) {}
// more stuff
}
import {ESService} from "./ESService";
import {Injectable, Inject} from '@nestjs/common';
@Injectable()
export class UniqueInteractionsService{
constructor(@Inject(ESService) private readonly esService: ESService) {}
// more stuff
}
esProvider.ts:
import {ESService} from "./esService";
export const EsProvider = {
provide: ESService,
useFactory: async () => {
const esService = new ESService();
await esService.init();
return esService;
}
};
您正在从SharedModule导出UniqueInteractionService。在目标模块中,您需要导入此SharedModule,然后通过依赖项注入在属于TargetModule的targetService中使用UniqueInteractionService 例如:
target-module.ts:
@Module({
imports: [SharedModule]
})
export class TargetModule {}