我使用多个存储库创建了服务:
@Injectable()
export class DataService {
constructor(
@InjectRepository(ClassicPrices, 'connect')
private classicPrices: Repository<ClassicPrices>,
@InjectRepository(ModernPrices, 'connect')
private modernPrices: Repository<ModernPrices>,
) {}
public pricesRepository(server): Repository<ModernPrices | ClassicPrices> {
switch (server) {
case 'modern':
return this.modernPrices;
case 'classic':
return this.classicPrices;
}
}
}
数据模块设置:
@Module({
imports: [
TypeOrmModule.forFeature([
ClassicPrices,
ModernPrices
], 'connect'),
],
providers: [DataService],
exports: [DataService]
})
export class DataModule {}
当我在另一个模块中使用它时^ 我遇到错误“Nest 无法解析 DataService (connect_ClassicPrices) 的依赖关系。请确保索引 [2] 处的参数 connect_ClassicPrices 在 DataModule 上下文中可用。
可能的解决方案:
@Module({
imports: [ /* the Module containing connect_ClassicPrices */ ]
})
如何导出存储库?
答案 0 :(得分:1)
如果您想在其他模块中使用来自 DataModule
的存储库,您需要将 TypeOrmModule
添加到 DataModule
中的导出数组:
exports: [DataService, TypeOrmModule]
然后,任何导入 DataModule
的模块都可以使用通过 TypeOrmModule.forFeature
注册的存储库。通常,最好只导出服务并将存储库的范围限制在其模块内,因为这会使您的应用更加模块化。