类型的参数不能分配给任何[]类型的参数

时间:2018-02-25 13:16:18

标签: angular typescript

我的代码存在一些问题,这是错误

  

消息:''员工'类型的论点不能分配给'类型的参数。任何[]'。 '包括' '员工'中缺少财产。类型。

这是服务



import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList, AngularFireObject } from 'angularfire2/database';
import { Observable } from 'rxjs';
import { Employee } from '../Employee';
import { FirebaseApp } from 'angularfire2';

@Injectable()
export class EmployeeService {
 
  employees: AngularFireList<any[]>;
  Employee: AngularFireObject<any>;

  constructor( 
    public af: AngularFireDatabase
  ) { 
    this.employees = this.af.list('/employees/employees') as AngularFireList <Employee[]>;
    
  }
getEmployees(): AngularFireList <any> {
  return this.employees ;

}
addEmployee(emp:Employee)
{
  
  return this.employees.push(emp);
}
}
&#13;
&#13;
&#13;

这是我的组件

&#13;
&#13;
import { Component, OnInit } from '@angular/core';
import { Employee } from '../../Employee';
import { FlashMessagesService } from 'angular2-flash-messages';
import { timeout } from 'q';
import { Router } from '@angular/router';
import { EmployeeService } from '../../services/employee.service';
@Component({
  selector: 'app-add-employee',
  templateUrl: './add-employee.component.html',
  styleUrls: ['./add-employee.component.css']
})
export class AddEmployeeComponent implements OnInit {
  employee:Employee={
    
    firstName:"",
    lastName:"",
    email:"",
    country:"",
    city:"",
    phone:0,
    salary:0
  }
  disableSalary:boolean=true;
  constructor(public fashMessagesService : FlashMessagesService, public router:Router,
  public employeeService : EmployeeService
  ) { }

  ngOnInit() {
    
  }


  mySubmit({value,valid}:{value:Employee,valid:boolean}){
    if(this.disableSalary){
      value.salary=0;
    }

    if (!valid) {
      this.fashMessagesService.show('Please Write Correct Info',{cssClass:'alert-danger',timeout:3000});
      //console.log("not correct data");
      this.router.navigate(['add-employee']);
    }else {
      this.employeeService.addEmployee(value);
      this.fashMessagesService.show('Added Successfully!',{cssClass:'alert-success',timeout:3000});
      this.router.navigate(['/']);
      //console.log(this.employee);

    }
  }

}
&#13;
&#13;
&#13;

我不知道究竟是什么问题,我希望有人得到它。

1 个答案:

答案 0 :(得分:1)

AngularFireList类型需要列表将包含的单一类型,数组类型。 employees数组的正确声明为employees: AngularFireList<Employee>(不是Employee[],而不是any[])。

它现在失败的原因是因为它希望您将any[]类型的内容传递给.push(),并且Employee无法分配给any[]