更新一系列对象内的对象属性
我正在尝试使用儿童组件中的EventEmitter在数组内部的对象中更新属性。
interface productItem {
keepOpen: boolean;
}
products$:productItem[] = Array(7).fill(
{
keepOpen:false
}
)
当用户单击按钮时,我正在使用此功能:
shouldKeepAddToBagOpen(dataFromChild: boolean, index: number): void {
// handle callback
this.keepAddToBagOpen = dataFromChild;
this.products$[index].keepOpen = dataFromChild;
}
但是它会影响数组中的所有项目,而不仅仅是具有给定 index
的一个功能:
我在做什么错?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当Array.fill通过一个对象时,它将复制引用并用对该对象的引用填充数组。
您可以使用以下内容:
When Array.fill gets passed an object, it will copy the reference and fill the array with references to that object.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#using_fill
You could instead use something like: