在我的组件中,我声明了一些这样的数据:
data() {
return {
defaultValue: {json object with some structure},
activeValue: {}
...
并在组件方法中复制此值:this.activeValue = this.defaultValue
但问题是,更改后 this.activeValue
值 a 在 this.defaultValue
中有变化也。如果我使用
Object.freeze(this.defaultValue)
并尝试改变this.activeValue
我得到错误 - 对象不可写。我如何复制数据但没有引用?
最佳答案
如果你有简单的对象,最快最简单的方法就是使用 JSON.parse 和 JSON.stringify;
const obj = {};
const objNoReference = JSON.parse(JSON.stringify(obj));
关于javascript - 如何在vue中复制没有引用的json对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62847820/