首页 > 解决方案 > 我如何在 NestJS 中从另一个类扩展一个类?

问题描述

我正在尝试从 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 导入它,但我得到了同样的错误。

标签: javascriptangulardependency-injectionnestjs

解决方案


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


推荐阅读