angular - TypeError : Cannot add property 1, 对象在 Array.push (<anonymous>) 处不可扩展↵

标签 angular typescript rxjs

我正在尝试向数组中添加一些数据,但出现不可扩展错误。

组件代码:

this._store.select(p => p.collection.workspaceCollectionPages).subscribe((data: CollectionPageDbModel[]) =>{
      if(data){
      return data.map((collectionPageDbModel)=> {

      this.collectionPages.push(collectionPageDbModel) //Getting error on this line while pushing

    });}

enter image description here

我的数据 var 中有四个对象,我试图将其推送到 collectionPages 中,但在推送时出现可扩展错误。

enter image description here 我需要添加更多数据的 collectionPage 数组

enter image description here 我需要推送的这些数据

CollectionPageDbModel:

export class CollectionPageDbModel implements IDbModel {
  public id?: number;
  public collection_version_id: number;
  public user_id?: string;
  public name: string;
  public position?: number = 0;
  public contents: string;
  public created_at?: string;
  public updated_at?: string;
  public server_id?: any;

}

谁能帮我解决这个问题

最佳答案

使用 Object.assign 方法复制对象并重试,

this.collectionPages = Object.assign([], this.collectionPages);
this.collectionPages.push(collectionPageDbModel);

ES6 简化代码是,

this.collectionPages = [...this.collectionPages, collectionPageDbModel]

关于angular - TypeError : Cannot add property 1, 对象在 Array.push (<anonymous>) 处不可扩展↵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492093/

相关文章:

node.js - grunt 构建中的问题

angular - 如何嵌套包裹在 APP_INITIALIZER 中的 httpclient 调用

angular - angular2 中 md-checkbox 的状态

javascript - TypeError : is not a function. 绑定(bind)此的正确方法。 JS 中的引用

html - 如何使用 ngModel 在 div 的 innerhtml 属性上实现 2 路数据绑定(bind)?

reactive-programming - Rxjs)有选择地取消订阅 flatMap 中的可观察量

angular - 在发出两个可观察对象的第一个值后,Zip 不发出值

javascript - 测试 rxjs : How to fake-click an element and check the resulting stream with marble diagrams?

php - 将带有 formData 的数据发送到 mysql

Angular 4 上下文在身份验证后丢失