在Employee“类型中缺少属性toggleEdit,但它是一种方法,该怎么办?Angular2

时间:2017-11-08 13:43:36

标签: angular typescript compiler-errors angular2-components

我有一个员工组件 -

export class EmployeeComponent{
name:string;
    isEditOn:boolean;

    constructor(){}

     toggleEdit():void{
        this.isEditOn = !this.isEditOn;
    }
}

这样的EmployeeList组件 -

export class EmployeeListComponent{
    employees: Employee[] = [];

    AddNewEmployee(){
        this.employees.push({name:"New Employee", isEditOn:false});
    }
}

我正在尝试在列表中推送新员工的行上出现错误。

  

类型'{name:string;}中缺少属性'toggleEdit'; isEditOn:false; }”。

但这不是一种方法吗?我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:1)



export class Employee {

    constructor(public name: string, public isEditOn: boolean){}

     toggleEdit():void{
        this.isEditOn = !this.isEditOn;
    }
}






export class EmployeeListComponent{
    employees: Employee[] = [];

    AddNewEmployee(){
        this.employees.push(new Employee("New Employee", false));
    }
}