angular - 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);
});
解决方案
似乎在附件控制台中,您的响应是对象数组。您不能直接从数组对象中获取 id 属性。需要指定索引。尝试这个
this.imageData = m;
this.imageId = this.imageData[0].Id;
console.log(m);
console.log('ImageId ' + this.imageId);
推荐阅读
- android - ChildEventListener 必须实现 onChildChanged
- python - 当第一个元素匹配时如何对嵌套列表进行排序?
- python-3.x - 打开 QComboBox 会触发 mouseMoveEvent 但不会触发 mousePressEvent
- python - lmfit 有没有办法只显示拟合曲线?
- servlets - @WebServlet 在 Netbeans 11.0 中失败并出现 java.lang.RuntimeException: com.example.NewServlet。
(NewServlet.java:1) - python - 轻松创建大量编号 URL
- javascript - let o1.prototype = Object.create(o2.prototype) 和 o1.prototype = o2.prototype 有什么区别?
- c++ - 你如何复制一个对象?
- python - 在嵌套的 Python 字典和列表中查找第一次出现的键
- java - 如何一对一匹配元素?