首页 > 解决方案 > ImageId 未分配给角度 11 中的变量

问题描述

我试图将 imageId 分配给一个变量,但它没有被分配。在控制台日志中,它显示undefined。下面是我尝试过的代码。我可以在控制台日志中记录我的对象。

对象结果

imageId: number;

this.imageService.getImage(this.productId).subscribe(m => {
  this.imageData = m;
  this.imageId = this.imageData.Id;
  console.log(m);
  console.log('ImageId ' + this.imageId);
});

标签: angular

解决方案


似乎在附件控制台中,您的响应是对象数组。您不能直接从数组对象中获取 id 属性。需要指定索引。尝试这个

  this.imageData = m;
  this.imageId = this.imageData[0].Id;
  console.log(m);
  console.log('ImageId ' + this.imageId);

推荐阅读