Angular无法分配为只读属性

时间:2018-12-06 11:35:30

标签: angular typescript

我想将值system更新为对象telecom,但是在此阶段它显示此错误

  

无法将对象'[object]分配为只读属性'system'   对象]'

this.organization.telecoms.forEach((telecom: Telecom) => 
   telecom.system = 'test'
);
console.log(this.organization);

export class Organization {
    public effectiveDate?: EffectiveDate;
    public identifiers?: IdentifierOrg[];
    public kilometerZones?: KilometerZone[];
    public telecoms?: Telecom[];
}

export class Telecom {
    public system?: string;
    public value?: string;
}

1 个答案:

答案 0 :(得分:-1)

尝试初始化课程。

here签出此应用

export class AppComponent  {
  organization: Organization;

  constructor() {
    this.organization = new Organization();
    this.organization.telecoms = [
      {
        system: 'foo',
        value: 'val'
      },
      {
        system: 'foo2',
        value: 'val2'
      },
      {
        system: 'foo3',
        value: 'val3'
      }
    ]
    this.organization.telecoms.forEach((telecom: Telecom) => 
      telecom.system = 'test'
    );
    console.log(this.organization);
  }
}

export class Organization {
    public telecoms?: Telecom[];
}

export class Telecom {
    public system?: string;
    public value?: string;
}