尝试实现接口时出现以下错误。
Build:Type '{ code: string; name: string; gender: string; annualSalary: number; dateOfBirth: string; }[]' is not assignable to type 'IEmployee[]'.
这是界面
//file employee.ts
export interface IEmployee {
code: string;
name: string;
gender: string;
annualSalary: number;
dateOfBirth: string;
//method
computeMonthlySalary(annualSalary: number): number;
}
export class Employee implements IEmployee {
constructor(public code: string, public name: string, public gender: string,
public annualSalary: number, public dateOfBirth: string) {
}
computeMonthlySalary(annualSalary: number): number {
return annualSalary / 12;
}
}
我正在尝试在employeelist.component.ts
中实现它//file employeelist.component.ts
import { Component } from '@angular/core';
import { IEmployee } from './employee';
@Component({
selector: 'list-employee',
templateUrl: 'app/employee/employeelist.component.html',
styleUrls: ['app/employee/employeelist.component.css']
})
export class EmployeeListComponent {
employees: IEmployee[];
// employees: any[]; //this works fine
selectedEmployeeCountRadioButton: string = 'All';
//getting error on this.employees
constructor() {
this.employees ={code:'emp101',name:'Tom',gender:'Male',annualSalary:95500,dateOfBirth:'12/6/1981'}];
}
}
请指导!
答案 0 :(得分:0)
您声明的对象没有computeMonthlySalary方法
试试这个
import { IEmployee, Employee } from './employee';
//...
this.employees =[new Employee('emp101','Tom','Male',95500,'12/6/1981')];