错误 TS2322:类型 'Object' 不可分配给类型 'Contact'。 Angular

error TS2322: Type 'Object' is not assignable to type 'Contact'. Angular

我想 运行 我的项目在我的前端保存联系人 我在我的 IDE webStorm 中读到这个错误 src/app/new-contact/new-contact 中的错误。component.ts(24,9):错误 TS2322:类型 'Object' 不可分配给类型 'Contact'。 'Object' 类型可分配给极少数其他类型。您是想改用 'any' 类型吗? 属性 'id' 在类型 'Object' 中缺失。

在 new-contact.component.ts(24,9) (at .subscribe((data)) 我有这个代码

saveContact(){
this.contactService.saveContact(this.contact)
  .subscribe((data)  => {
    this.contact = data;
    this.mode = 2;
  }, err=>{
    console.log("ErReUr : "+err);
  });

}

我有服务代码

saveContact(contact: Contact){
return this.http.post("http://localhost:8080/addPerson", contact);
}

我在项目中有一个 class,但在项目的一个文件中,这是代码

export class Contact {
   any = null;
   nom: string = '';
   prenom: string = '';
   email: string = '';
   tel: number = 0;
   photo: string = '';
   dateNAissance:Date;
 }

问题出在 联系人 的 ID 和对象

帮助我,我想要一个解决方案,谢谢 :)

试着这样写你的服务:

saveContact(contact: Contact){
  return this.http.post("http://localhost:8080/addPerson", contact) as Observable<Contact>;
}

即return 它作为一个 Contact 类型的 Observable。