如何为多个模块使用一个公共服务?
我有服务
@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,
) {}
错误:
- 如果 TestService 是提供者,它是当前 ProductsModule 的一部分吗?
- 如果 TestService 是从单独的 @Module 导出的,那么该模块是否会导入 ProductsModule 中?
@Module({
imports: [ /* the Module containing TestService */ ]
})