I've a class Contact:
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;
}
I have another class SelectedContact which is inheriting this class
export class SelectedContact extends Contact {
public skipMessages: number;
public hasMessages: boolean;
public canSendMessage: boolean;
}
I have a service which is to get and set the 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;
}
}
Then inside my component's ngOnInit(), I'm trying to set my local variable, but getting error:
@Component({...})
export class RightSidebarComponent implements OnInit {
selectedContact: SelectedContact;
ngOnInit() {
this.selectedContact = this.contactService.SelectedContact;
}
}
I'm getting following error: "ERROR TypeError: Cannot set property selectedContact of [object Object] which has only a getter", in the above line.
It looks like this.selectedContact
is defined as getter in your component.