我正在尝试向数组中添加一些数据,但出现不可扩展错误。
组件代码:
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
});}
我的数据 var 中有四个对象,我试图将其推送到 collectionPages 中,但在推送时出现可扩展错误。
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/