错误TypeError:无法设置只有吸气剂的[object Object]的selectedselectedContact属性

时间:2018-09-20 07:27:47

标签: angular typescript

我有一个班级联系人:

export class Contact
{
  public Id: string = '';
  public FirstName: string = '';
  public LastName: string = '';
  public Email: string = '';
  public Description: string = '';
  public Members: Array<User> = [];
  public Mute: boolean = false;   
}

我还有另一个类SelectedContact,它正在继承该类

export class SelectedContact extends Contact {
    public skipMessages: number;
    public hasMessages: boolean;
    public canSendMessage: boolean;
}

我有一项服务,用于获取和设置SelectedContacts

@Injectable()
export class ContactService
{
  private contactList: Array<Contact> = [];
  private selectedContactId: string;

  public get SelectedContact(): SelectedContact
  {
    const contact: Contact = this.contactList.find((v) => v.Id === this.selectedContactId);

    return contact ? contact : null;
  }

  public set SelectedContact(value: string | Contact): void
  {
    this.selectedContactId = typeof value === 'string' ? value as string : value.Id;
  }
}

然后在组件的ngOnInit()中,尝试设置局部变量,但出现错误:

@Component({...})
export class RightSidebarComponent implements OnInit {
    selectedContact: SelectedContact;
    ngOnInit() {
      this.selectedContact = this.contactService.SelectedContact;
    }
}

我收到以下错误:在上一行中,“错误TypeError:无法设置仅具有吸气剂的[object Object]的属性selectedContact”。

1 个答案:

答案 0 :(得分:0)

似乎this.selectedContact在您的组件中被定义为吸气剂。