我正在尝试在 NestJS 中导出和导入服务。这看起来很简单,我认为它应该像这样工作,但我收到一条错误消息,指出 Nest 无法解析依赖项。
设置模块
该模块具有应导入的服务,并将其导出。
@Module({
imports: [
MongooseModule.forFeature([{ name: Setting.name, schema: SettingSchema }]),
],
providers: [SettingsService],
exports: [SettingsService],
})
export class SettingsModule {}
MsgraphModule
这个模块应该通过模块导入服务,因为服务被注入(inject)到他们的服务中。
@Module({
imports: [SettingsModule],
providers: [MsgraphService],
})
export class MsgraphModule {}
应用程序模块
@Module({
imports: [
MongooseModule.forRoot('mongodb://localhost/lead-import', {
useCreateIndex: true,
}),
MsgraphModule,
SettingsModule,
...
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
我在这里做错了什么?
最佳答案
问题是我使用了 @Inject() 装饰器,它只需要自定义依赖注入(inject)。
@Injectable()
export class MsgraphService {
private client: Client;
private authenticator;
constructor(@Inject() private settingsService: SettingsService) {
this.init();
this.authenticator = new MSGraphAuthenticator();
}
...
}
因此删除@Inject()
就可以了。
关于javascript - Nest 无法解析对导出服务的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67817796/