angular2数据网格属性';id';不';类型字符串中不存在
我正在尝试实现angular2 datagrid followig 我的模型:angular2数据网格属性';id';不';类型字符串中不存在,angular,typescript,datagrid,Angular,Typescript,Datagrid,我正在尝试实现angular2 datagrid followig 我的模型: export class Inventory{ active: boolean; constructor(public id: number, public article: string, public description: string, public quantity: number, public price : string
export class Inventory{
active: boolean;
constructor(public id: number,
public article: string,
public description: string,
public quantity: number,
public price : string,
public imgURL: string,
public createdAt: string,
public updatedAt: string) {
this.active = true;
}
}
ngOnInit中的我的组件:
this.getAllInventory();
this.table = new NgDataGridModel<Inventory>([]);
for (let inv in this.allInventories) {
this.table.items.push(new Inventory(
inv.id,
inv.article,
inv.description,
inv.quantity,
inv.price,
inv.imgURL,
inv.createdAt,
inv.updatedAt)
);
}
这里是调用服务的方法,此方法返回清单的json数组:
getAllInventory(){
var headers = new Headers();
headers.append("Content-Type", 'application/json');
headers.append('Authorization', `Bearer ${this.globalVar.getToken()}`);
var options = new RequestOptions({ headers: headers });
var result = this.http
.get(this.globalVar.getHost() + "inventory", options)
.map((response: Response) => response.json());
return result;
}
我的错误:在de for loop中,对象inv位于其atributes中,请告诉我:类型string中不存在属性“id”,所有atributes也是如此。我怎样才能修好它 这意味着Angular 2模型中使用的库存中每个属性的类型与服务器数据的类型不匹配 两种选择
data.forEach((item) => {
tempInventory=new Inventory();
tempInventory.id: _.toInteger(item.id),
tempInventory.article: item.article.toString(),
tempInventory.description: item.description.toString(),
tempInventory.quantity: _.toInteger(item.quantity),
tempInventory.price : item.price.toString(),
tempInventory.imgURL: item.imgURL.toString(),
tempInventory.createdAt: item.createdAt.toString(),
tempInventory.updatedAt: item.updatedAt.toString()
this.allInventories.push(tempInventory);
})
console.log(this.allInventories);
},
err=>{
alert ("Error getting inventories");
console.error(err);
});
}
你能分享你的
getAllInventory()吗
method?它可能会返回string
我在@echonax:DWhere-you-initializethis.allInventories
?您的代码是否正常工作?在for循环之前,您不会收到getAllInventory
的响应。这是一个异步问题。您的第二个选项看起来不错,但我遇到了这个错误:Can not读取未定义的属性push。我想是因为异步进程可能在。在哪里进行此调用?在Init上?
data.forEach((item) => {
tempInventory=new Inventory();
tempInventory.id: _.toInteger(item.id),
tempInventory.article: item.article.toString(),
tempInventory.description: item.description.toString(),
tempInventory.quantity: _.toInteger(item.quantity),
tempInventory.price : item.price.toString(),
tempInventory.imgURL: item.imgURL.toString(),
tempInventory.createdAt: item.createdAt.toString(),
tempInventory.updatedAt: item.updatedAt.toString()
this.allInventories.push(tempInventory);
})
console.log(this.allInventories);
},
err=>{
alert ("Error getting inventories");
console.error(err);
});
}