我如何在 NestJS 中从另一个 class 扩展一个 class?

How can I in NestJS extend a class from another class?

我正在尝试从 NestJS 中的另一个 class 服务扩展 class 服务,并使用 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 { }

最后一个 class 我假装要扩展的是:

@Injectable()
export class CryptoService {

    constructor() { }

}

@Injectable()
export class CoinMakerService extends CryptoService {

    constructor() {
        super()
    }
}

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

由于 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

我也尝试过这些导出,但它们没有用。事实上,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.

已解决。问题出在 tsconfig.json 中的 paths NestJS 似乎与我经常使用的这个功能不太兼容。