【问题标题】:How can I in NestJS extend a class from another class?我如何在 NestJS 中从另一个类扩展一个类?
【发布时间】:2021-07-24 15:00:56
【问题描述】:

我正在尝试从 NestJS 中的另一个类服务扩展一个类服务,并使用 DI 将其加载到第三个服务中。我得到嵌套错误:

 Error: Nest can't resolve dependencies of the AlgorithmsService (?, SocketService). Please make sure that the argument Object at index [0] is available in the AlgorithmsModule context.

Potential solutions:
- If Object is a provider, is it part of the current AlgorithmsModule?
- If Object is exported from a separate @Module, is that module imported within AlgorithmsModule?
  @Module({
    imports: [ /* the Module containing Object */ ]
  })

所以如果我检查 algorithm.service.ts


@Injectable()
export class AlgorithmsService {

    constructor(public coinMakerService: CoinMakerService,
        public socketService: SocketService) { }
}     

在 app.module.ts 中

@Module({
  imports: [AlgorithmsModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

所以 algorithm.module.ts

@Module({
    imports: [],
    exports: [],
    controllers: [AlgorithmsController],
    providers: [
        AlgorithmsService,
        CoinMakerService,
        CryptoService,
        SocketService
    ]
})
export class AlgorithmsModule { }

最后是我假装 tyo 扩展的最后一个类:

@Injectable()
export class CryptoService {

    constructor() { }

}

@Injectable()
export class CoinMakerService extends CryptoService {

    constructor() {
        super()
    }
}

我不知道我是否需要声明其他任何内容。我试图将所有服务移动到一个共享模块并从顶级 app.module.ts 导入它,但我得到了同样的错误。

【问题讨论】:

    标签: javascript angular dependency-injection nestjs


    【解决方案1】:

    由于AlgorithmsService 留在另一个模块中,您需要在AlgorithmsModule 中公开您可以在其外部使用的提供程序

    @Module({
        imports: [],
        exports: [CoinMakerService], // I'm not sure if you need to add CryptoService as well
        controllers: [AlgorithmsController],
        providers: [
            AlgorithmsService,
            CoinMakerService,
            CryptoService,
            SocketService
        ]
    })
    export class AlgorithmsModule { }
    

    请阅读文档https://docs.nestjs.com/modules

    【讨论】:

      【解决方案2】:

      我也尝试过这些导出,但它们不起作用。事实上,SocketService 没有给出错误...如果我在构造函数中删除extend CryptoServicepublic coinMakerService: CoinMakerService 它可以工作...我不知道是否真正失败的是我需要在一些提供或某种特殊注射器的方式,因为我测试的任何东西都不起作用。总是同样的错误...

      @Module({
          imports: [],
          exports: [
              CoinMakerService,
              CryptoService
          ],
          controllers: [AlgorithmsController],
          providers: [
              AlgorithmsService,
              CoinMakerService,
              CryptoService,
              SocketService
          ]
      })
      export class AlgorithmsModule { }
      
      Nest can't resolve dependencies of the AlgorithmsService (?, SocketService). Please make sure that the argument Object at index [0] is available in the AlgorithmsModule context.
      
      Potential solutions:
      - If Object is a provider, is it part of the current AlgorithmsModule?
      - If Object is exported from a separate @Module, is that module imported within AlgorithmsModule?
        @Module({
          imports: [ /* the Module containing Object */ ]
        })
       Error: Nest can't resolve dependencies of the AlgorithmsService (?, SocketService). Please make sure that the argument Object at index [0] is available in the AlgorithmsModule context.
      

      【讨论】:

        【解决方案3】:

        解决了。问题出在pathstsconfig.json 看来 NestJS 与我经常使用的这个功能不太兼容。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-01
          • 1970-01-01
          相关资源
          最近更新 更多