typescript - 我应该在哪个文件中放置一个返回新子类的开关函数
问题描述
我有一个父类
export class Component{
}
和许多儿童班
export class ComponentOne extends Component{
}
export class ComponentTwo extends Component{
}
我想在 Component 中创建一个基于数据返回正确类型的类的函数
parseComponentDatabase(databaseObject){
switch(databaseObject.type){
case "One":
return new ComponentOne(databaseObject)
case "Two":
return new ComponentTwo(databaseObject)
}
}
我想将该函数放在 Component 中,但最终导致循环依赖,因为这使 Component 导入了它的子级,并且它的子级已经导入了它。在我的打字稿项目中禁止循环依赖。
我的问题是,我必须把这个函数放在哪个文件中?
解决方案
您需要使用工厂来实例化您的对象,而不是在组件类中添加它,请参阅工厂模式文档。
推荐阅读
- python - Python XGB 标签未读取正确的标签大小
- java - Esper 数据流中的“Where 子句”错误
- parsing - 如何跨 *nix TTY/终端仿真器解释/解析键盘和鼠标输入?
- r - 在R中的列表中查找行中最频繁的元素
- javascript - 离子虚拟滚动渲染延迟
- javascript - 从打印 java-script window.print 中删除应用程序名称和网页地址
- python - 用python从json内容中提取一些数据
- javascript - 如何使用快速服务器为 Web 组件应用程序提供服务
- html - 在选择选项前添加 svg 图像
- autodesk-forge - Forge DA for Revit 多个版本