首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 2接口-实现错误

Angular 2接口-实现错误
EN

Stack Overflow用户
提问于 2018-01-19 21:30:44
回答 1查看 38关注 0票数 0

我在尝试实现一个接口时遇到了以下错误。

代码语言:javascript
复制
Build:Type '{ code: string; name: string; gender: string; annualSalary: number; dateOfBirth: string; }[]' is not assignable to type 'IEmployee[]'.

这就是界面

代码语言:javascript
复制
//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中实现它

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

请指点一下!

EN

回答 1

Stack Overflow用户

发布于 2018-01-19 21:44:54

您声明的对象没有computeMonthlySalary方法

尝尝这个

代码语言:javascript
复制
import { IEmployee, Employee } from './employee';
//...
this.employees =[new Employee('emp101','Tom','Male',95500,'12/6/1981')];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48342268

复制
相关文章

相似问题

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