【问题标题】:Nestjs import dependecy not working to import my serviceNestjs导入依赖项无法导入我的服务
【发布时间】:2021-05-13 15:56:36
【问题描述】:

我在其他模块中导入了一个模块,但可以在其他上下文中访问该服务

导出时的模块

@Module({
  controllers: [ModuleController],
  providers: [
    ModulesAcessService,
    ModulesRepository,
    ModulesService
  ],
  exports: [ModulesService, ModulesRepository, ModulesAcessService],
  imports: [
    TypeOrmModule.forFeature([ModuleEntity]),
    forwardRef(() => CoursesModule),
    forwardRef(() => LessonsModule)
  ]
})
export class ModulesModule { }

我导入时的模块 - ModulesModule

@Module({
  controllers: [LessonsController],
  providers: [
    LessonsService,
    LessonsAcessService,
    LessonsRepository,
    UserLessonsService,
    UserLessonsRepository,
    UserLessonsSubscriber,
    UserLessonsEvents
  ],
  exports: [LessonsRepository,LessonsService, LessonsAcessService, UserLessonsService],
  imports: [
    TypeOrmModule.forFeature([Lesson, UserLesson]),
    forwardRef(() => CoursesModule),
    forwardRef(() => ModulesModule)
  ]
})
export class LessonsModule { }

错误信息

Nest can't resolve dependencies of the LessonsService (LessonsAcessService, UserLessonsService, UrlHelper, LessonsRepository, CoursesService, ?). Please make sure that the argument dependency at index [5] is available in the LessonsModule context.

我的服务中的导入 - 我无法访问 ModulesService。

import { ModulesService } from '../modules/modules.service';

@Injectable()
export class LessonsService {

  constructor(
    private lessonsAccessService: LessonsAcessService,
    private userLessonsService: UserLessonsService,
    private urlHelper: UrlHelper,
    private lessonsRepository: LessonsRepository,
    private coursesService: CoursesService,
    private modulesService: ModulesService
  ) { }
...

  • 节点版本 14.7.0

  • Nestjs 版本 7

【问题讨论】:

  • 您的ModulesService 是否导入您的LessonsService
  • 是的,我在 ModulesService 中导入 LessonsService 来使用。
  • 你试过这个吗:docs.nestjs.com/fundamentals/…

标签: node.js nestjs


【解决方案1】:

您也需要将forwardRef 用于服务,而不仅仅是模块。在您的ModulesService 中,在lessonsService 上使用@Inject(forwardRef(() => LessonsService))。在LessonsService 中,在modulesService 上使用@Inject(forwardRef(() => ModulesService))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多