我想使用一个具有接口"entity“的类"element”。我想使用像c#接口一样的接口。我有一个带有输入的角度组件。输入类型是entity (接口),因为angular组件有两个输入类。我想我可以使用像c#这样的接口,在其中定义我在其他类(例如:" element ")中实现的方法,并在强制转换为接口的element实例上调用此方法。我希望你能理解我想做什么。
元素类:
import { Entity } from "./entity";
export class Element implements Entity {
public Id: string;
public Name: string;
public ShortDesc: string;
public Description: string;
public Picture: any;
public GetId(): string {
return
}
public GetName(): string {
return this.Name;
}
public GetType(): string {
return typeof(this);
}
}
实体类:
export interface Entity {
GetId(): string;
GetName(): string;
GetType(): string;
}
我使用angular material 2中的对话框。我给对话框正确的输入类型"element“(基类)。
deleteClick(ele: Element) {
let dialogRef = this.dialog.open(DeleteDialog, {
height: '400px',
width: '400px',
data: {
entity: ele
}
});
在该对话框中:
export class DeleteDialog implements OnInit {
ngOnInit() {
}
dialogResult: string = 'cancel';
constructor(
public dialogRef: MatDialogRef<DeleteDialog>,
@Inject(MAT_DIALOG_DATA) public data: {
type: string,
entity: Entity
},
private globalServ: GlobalService) {
let entity = this.data.entity;
let name = entity.GetName();
//Here I get the Exception ERROR TypeError: entity.GetName is not a function
}
}
我的错误在哪里?我收到错误消息"ERROR TypeError: entity.GetName is not a function“
发布于 2020-08-27 15:31:01
请尝试调用如下所示的函数
let name = entity['GetName()']https://stackoverflow.com/questions/47438485
复制相似问题