我正在使用依赖注入系统(inversifyJS)在TypeScript中构建一个库来构建自身并在内部解析依赖关系。 我遇到的问题是 - 我想将多个实例从注入系统暴露给库的消费者。
目前我要做的是:
import kernel from "./src/inversify.config";
import EntityManager from './src/manager/entityManager.service';
import StorageService from './src/storage/storage.service';
import LanguageService from './src/language/language.service';
export { kernel.get<EntityManager>(EntityManager) as EntityManagerInstance };
export { kernel.get<EntityManager>(LanguageService) as LanguageServiceInstance };
export { kernel.get<EntityManager>(StorageService) as StorageServiceInstance };
我看到的解决方案是使用Facade导出类型并稍后访问它们:
import EntityManager from './src/manager/entityManager.service';
import StorageService from './src/storage/storage.service';
import LanguageService from './src/language/language.service';
import InjectionFacade from './utils/injection.facede';
export { EntityManager, LanguageService, StorageService, InjectiorFacade };
// Usage:
// import {InjectionFacade, StorageService} from 'entity-manager';
// let injectionFacade: InjectionFacade = InjectionFacade.createAndResolve();
// let storageService: StorageService = injectionFacade.getStorageService();
但问题是我还有一个无用的抽象
有没有办法在不丢失类型定义和导出即用型对象的情况下实现这种解决方案?
答案 0 :(得分:3)
您想要使用多声明变量,例如
export const
EntityManagerInstance = kernel.get<EntityManager>(EntityManager),
LanguageServiceInstance = kernel.get<EntityManager>(LanguageService),
StorageServiceInstance = kernel.get<EntityManager>(StorageService);