简体   繁体   English

Vue JS-将变量设置为vueObject.dataItem并对其进行修改也会修改Vue数据项

[英]Vue JS - Setting variable to vueObject.dataItem and modifying it also modifies the Vue data item

This behavior is causing problems in my projects. 此行为导致我的项目中出现问题。 This is a simplified version of what's happening. 这是正在发生的事情的简化版本。 I'd like to learn why this happens, and how I can avoid it. 我想了解为什么会发生这种情况,以及如何避免这种情况发生。 I have vue loaded in the head of my website: 我在网站的头部加载了vue:

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>

And this is the Vue object: 这是Vue对象:

vueObject = new Vue({
el: '#app',
data: {
"array": []
}
});

Now, my issue is this. 现在,我的问题是这个。 How do I save a copy of the “array” data item from the Vue Object so I can manipulate it without actually modifying the real array Vue data item? 如何从Vue对象保存“数组”数据项的副本,以便可以在不实际修改实际数组Vue数据项的情况下进行操作? This is what I'm trying to do, but it isn't working as intended; 这是我正在尝试做的,但是没有按预期工作。

var arrayCopy = vueObject.array;
arrayCopy.push("x");

This is causing both arrayCopy and vueObject.array to be set to [“x”]. 这导致arrayCopy和vueObject.array都设置为[“ x”]。 I only want to add “x” to arrayCopy. 我只想将“ x”添加到arrayCopy。 How can I do that? 我怎样才能做到这一点? Why does this happen? 为什么会这样?

Objects are assigned by reference . 对象通过引用分配 When you assign an array, you are just making another variable refer to the same object. 当您分配数组时,您只是在使另一个变量引用同一对象。 Modifying the contents of an object will be reflected in everything that references that object. 修改对象的内容将反映在引用该对象的所有内容中。

An array copy would be arrayCopy = vueObject.array.slice() , but the same caveat applies to any Objects contained in the array: if you modify their contents, those modifications will show up in both arrays. 数组副本将为arrayCopy = vueObject.array.slice() ,但相同的警告适用于数组中包含的任何对象:如果修改其内容,则这些修改将同时出现在两个数组中。 See more answers in the linked thread about deep copying. 在链接的线程中查看有关深度复制的更多答案。

You're referencing the vueObject.array by doing var arrayCopy = vueObject.array; 你引用vueObject.arrayvar arrayCopy = vueObject.array; not copying it to arrayCopy , so to avoid that use let arrayCopy = vueObject.array.slice(); 不要将其复制到arrayCopy ,因此要避免使用let arrayCopy = vueObject.array.slice(); or Object.assign(arrayCopy, vueObject.array) Object.assign(arrayCopy, vueObject.array)

 let vueObject = new Vue({ el: '#app', data: { array: ['a', 'b'] } }); let arrayCopy = vueObject.array.slice(); arrayCopy.push('x'); console.log(arrayCopy); console.log( vueObject.array); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.4/vue.min.js"></script> <div id="app"></div> 

Another option is to create a new Array and spread the vue data array inside the new array so we copy all its original values, but then manipulate it as it's own context. 另一个选择是创建一个新的数组并将vue数据数组散布在新数组内,以便我们复制其所有原始值,然后将其作为自己的上下文进行操作。

new Vue({
  el: "#app",
  data: {
    array: [1, 2, 3]
  },
  created: function() {
    this.doSomething();
  },
  methods: {
    doSomething() {
      const copy = [...this.array];
      copy.push(4);
      console.log("copy:", copy);
      console.log("origninal", this.array);
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM