如何为多个模块使用公共服务?
我有服务
@Injectable()
export class TestService {
test(): number {
return 123;
}
}我在App模块注册了。
providers: [TestService],
exports: [TestService]我想使用它在产品模块和其他。
@Module({
imports: [TestService],
controllers: [ProductsController],
providers: [ProductsService]
})在产品模块中使用
constructor(
@Inject('TestService')
private readonly TService: TestService,
) {}错误:
@Module({
imports: [ /* the Module containing TestService */ ]
})发布于 2020-12-14 09:48:18
您应该导入应用程序模块以使用serviceTest:
@Module({
imports: [AppModule],
controllers: [ProductsController],
providers: [ProductsService]
})但是如果想了解更多信息,就不能解决循环依赖问题--访问:循环依赖
因此,解决方案是创建一个共享模块,并使用它们,您应该只导入模块,而不是服务,exp:
@Module({
imports: [SharedModule],
controllers: [ProductsController],
providers: [ProductsService]
})关于共享模块共享模块的更多信息
https://stackoverflow.com/questions/65286331
复制相似问题