我有一个基本的通用组件,我想从基类中调用派生方法,该基类返回属于派生类的数据。
第一个问题是,我无法在抽象类中将其静态定义!
第二个问题是,当我在基类通用类中创建带有{}作为TDomain的派生类的新实例时,其空值和方法返回null ref错误!
基本实体是这样的:
export abstract class BaseEntity
{
abstract getColumns():string[];
}
我的模型是这样的:
export class Person extends BaseEntity
{
getColumns() {
return ["Id", "Name"];
}
}
我有一个通用的基本组件:
@Component({
selector: 'base-component',
templateUrl: './base.component.html',
styleUrls: ['./base.component.css']
})
export class BaseComponent<TDomain extends BaseEntity,TService extends DataService > implements OnInit {
data:any[];
constructor() {
}
ExportToExcel(): void {
//must create an instance of derived class
//then call getcolumns method of it
let obj = {} as TDomain; // but its empty doesnt work
ExcelService.export(obj.getcolumns(this.data));
}
}
及其派生组件:
@Component({
selector: 'Derived-Cmp',
templateUrl: './derived.component.html',
styleUrls: ['./derived.component.css']
})
export class DerivedComponent extends BaseCrudComponent<Person,PersonService> implements OnInit {
//ExportToExcel Method called form html
}
答案 0 :(得分:0)
要创建TDomain的实例,您需要将TDomain的构造函数传递给基类:
f["text"] = [str(x).replace(':',' ') for x in df["text"]]
链接表单github存储库call derived class method inside base generic class