添加新对象替换旧对象的内容 - VueJS Store [英] Adding new objects replaces content of old objects - VueJS Store

查看:22
本文介绍了添加新对象替换旧对象的内容 - VueJS Store的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将一个新对象插入到数组items"中.但问题是当我插入一个新对象时,它会替换之前添加的对象的内容.这样,即使添加的对象不同,也始终包含相同的对象.

I'm using the following code to insert a new object into the array 'items'. But the problem is when I insert a new object, it replaces the content of the object that is added just before. In such way, the always contains the same objects, even though added objects are different.

我听说这是由于使用了push",它也传递了引用.我该如何解决这个问题是 VueJS

I heard it's due to using 'push' which also passes the reference. How can I fix this is VueJS

Store.js

var store = new Vuex.Store({
  state: {
    value: 1,
    quote: {
      items: [],
      something: ''
    }
  },
  mutations: {
    ADD (state, item) {
      state.quote.items.push(item)
    }
  }
})

推荐答案

我自己终于得到了答案.解决方案是字符串化对象并将其解析回来,都使用 JSON"

Finally got the answer myself. The solution is 'stringify the object and parse it back, both using JSON'

  mutations: {
    ADD (state, item) {
      item = JSON.stringify(item)
      item = JSON.parse(item)
      state.quote.items.push(item)
    }
  }

这篇关于添加新对象替换旧对象的内容 - VueJS Store的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆