首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Typescript接口调用方法

Typescript接口调用方法
EN

Stack Overflow用户
提问于 2017-11-22 23:33:10
回答 1查看 2.1K关注 0票数 3

我想使用一个具有接口"entity“的类"element”。我想使用像c#接口一样的接口。我有一个带有输入的角度组件。输入类型是entity (接口),因为angular组件有两个输入类。我想我可以使用像c#这样的接口,在其中定义我在其他类(例如:" element ")中实现的方法,并在强制转换为接口的element实例上调用此方法。我希望你能理解我想做什么。

元素类:

代码语言:javascript
复制
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);
    }
}

实体类:

代码语言:javascript
复制
export interface Entity {
    GetId(): string;
    GetName(): string;
    GetType(): string;
}

我使用angular material 2中的对话框。我给对话框正确的输入类型"element“(基类)。

代码语言:javascript
复制
    deleteClick(ele: Element) {    
        let dialogRef = this.dialog.open(DeleteDialog, {
        height: '400px',
        width: '400px',
        data: {
            entity: ele
        }
    });

在该对话框中:

代码语言:javascript
复制
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“

EN

回答 1

Stack Overflow用户

发布于 2020-08-27 15:31:01

请尝试调用如下所示的函数

代码语言:javascript
复制
let name = entity['GetName()']
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47438485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档