node.js - 如何通过 Inputs() 将对象传递给子组件,但在父组件中更改时从不更新输入
问题描述
我通过 Inputs() 将一个对象从父组件传递给子组件。我知道是因为我传递的是一个对象而不是原始类型,它传递的是对该对象的引用。因此,当对象在父对象中发生变化时,我会看到它反映在子对象中。
通过 Inputs() 传递对象的最佳方法是什么,当它在父组件中发生更改时不会更新子组件?
解决方案
您需要使用两个属性。一个是您修改的,另一个是传递给子组件但是原始组件的克隆。
@Component({..})
export class MyComponent implements OnInit {
// the original object value
public value: any;
// a value used by child components
public forChild: any;
public OnInit() {
// use deconstruction to make a copy
this.forChild = {...this.value};
// use assign to make a copy
this.forChild = Object.assign({}, this.value);
// use JSON to make a deep copy
this.forChild = JSON.parse(JSON.stringify(this.value));
}
}
推荐阅读
- reactjs - reactjs在哪里添加axios拦截器
- prolog - 如何告诉 Prolog 两个文字是等价的
- python - 使用查找比较数据并使用python仅输出数据中最长的短语?
- python - Plotly/Dash -- 范围过滤器在 hoverData 上重置
- c# - 无法添加或更新 Azure AD 应用程序
- postgresql - POSTGIS - 查找给定点(经度,纬度)是否在表格的地理列(多边形的表示)内
- java - Hibernate 在选择之前不会刷新持久性上下文
- linux - 使用 ffmpeg 直播视频
- python - 为从数据库检索的数据创建动态 html 链接
- react-native - ReactNative 错误“任务执行失败”