Typescript Nestjs依赖注入-将服务注入服务
我有一个服务,我可以注入到其他组件没有任何问题 当我尝试将该服务注入另一个服务时,我得到Typescript Nestjs依赖注入-将服务注入服务,typescript,dependency-injection,nestjs,Typescript,Dependency Injection,Nestjs,我有一个服务,我可以注入到其他组件没有任何问题 当我尝试将该服务注入另一个服务时,我得到 Error: Nest can't resolve dependencies of the AService (?). Please make sure that the argument BService at index [0] is available in the AService context. 我找不到任何方法将服务注入彼此。这是否不受支持,有点反模式 如果是这样的话,如何处理我希望在我的
Error: Nest can't resolve dependencies of the AService (?).
Please make sure that the argument BService at index [0] is available in the AService context.
我找不到任何方法将服务注入彼此。这是否不受支持,有点反模式
如果是这样的话,如何处理我希望在我的应用程序的所有组件和服务中都可以使用的功能
代码如下:
b、 模块1.ts
import { Module } from '@nestjs/common';
import { BService } from './b.service';
@Module({
imports: [],
exports: [bService],
providers: [bService]
})
export class bModule { }
import { Module } from '@nestjs/common';
import { SensorsService } from './a.service';
import { SensorsController } from './a.controller';
import { BModule } from '../shared/b.module';
@Module({
imports: [BModule],
providers: [AService],
controllers: [AController],
exports: []
})
export class AModule {
b、 服务台
import { Injectable } from '@nestjs/common';
@Injectable()
export class BService {
someFunc();
}
a、 模块1.ts
import { Module } from '@nestjs/common';
import { BService } from './b.service';
@Module({
imports: [],
exports: [bService],
providers: [bService]
})
export class bModule { }
import { Module } from '@nestjs/common';
import { SensorsService } from './a.service';
import { SensorsController } from './a.controller';
import { BModule } from '../shared/b.module';
@Module({
imports: [BModule],
providers: [AService],
controllers: [AController],
exports: []
})
export class AModule {
}
a、 service.ts-哪个应该能够使用b.service
import { Injectable } from '@nestjs/common';
import { BService } from '../shared/b.service';
@Injectable()
export class AService {
constructor(
private bService: BService
) {}
someOtherFunc() {}
}
根据您的错误,您在某个地方的
imports
数组中有AService
,这不是在NestJS中做的事情。打破它
错误:嵌套无法解析AService(?)的依赖项
请确保索引[0]处的参数BService在AService上下文中可用
第一部分显示有困难的提供者,以及未知依赖项所在的?
。在这种情况下,AService
是无法实例化的提供程序,BService
是未知的依赖项
错误的第二部分是显式调用构造函数中的注入令牌(通常是类名)和索引,然后在模块上下文嵌套中查看。你可以读到Nest说的
在AService
上下文中
这意味着Nest正在查看一个名为AService
的模块。正如我之前所说,这是你不应该做的事情
如果您需要另一个模块中的
AService
,则应将AService
添加到AModule
的exports
数组中,并将AModule
添加到新模块的imports
数组中。我注意到声明代码中的模块是bModule
,但是您正在导入b模块
——这就是问题所在吗?与b服务相同
JavaScript区分大小写。确保您的类和实例具有适当的案例。另外,如果不是这样的话,您能说明这些模块是如何绑定到您的AppModule
的吗?@hlfrmn我的模块当然有一些其他名称。但是,由于它在通过代码后突然开始工作,我认为它是这样的失败。