【问题标题】:Angular Cannot assign to read only propertyAngular 无法分配给只读属性
【发布时间】:2019-05-08 02:24:09
【问题描述】:

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

无法分配给对象“[对象”的只读属性“系统” 对象]'

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;
}

【问题讨论】:

  • Object.defineProperty(telecom, 'system', { value: 'test' });
  • (或者你删除readonly属性)
  • @trichetriche 谁可以删除只读属性?
  • 尝试从foreach中删除: Telecom
  • 也可以试试forEach((telecom: any)

标签: angular typescript


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 2020-02-17
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多