为什么我会得到"Nest can't resolve dependencies"



我试图弄清楚为什么我会收到以下错误:

Nest 无法解析 UniqueInteractionsService (?( 的依赖关系。 请确保索引 [0] 处的参数依赖项为 在共享模块上下文中可用。

我的班级:

sharedModule.ts:

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
}

uniqueInteractionsService.ts:

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 导出 UniqueInteractionsService。在目标模块中,您需要导入此共享模块,然后您将能够通过依赖注入在属于目标模块的目标服务中使用 UniqueInteractionsService。

例:

target-module.ts:
@Module({
imports: [SharedModule]
})
export class TargetModule {}

最新更新