Vue-使用vuex进行深度复制在vuejs中不起作用

时间:2019-11-03 16:39:45

标签: vue.js

有 当我在项目中使用Vue + Vuex时,我发现了一个问题, 我的代码:

商店:

const state = {
  createInfo: {
    id: 0,
    // group_id: rootState.home.currentGroup,
    content: '',
    image_info: [],
    video_info: [],
    lat: '',
    lng: '',
    type: 1
  },
  list: [],
  canLoad: true 
}

突变功能:

setPublishInfoImages(state, {key, value}) {
    if (key === 'push') {
      state.createInfo.image_info.push(value)
    } else {
      state.createInfo.image_info.splice(key, 1)
    }
  },
页面上的

第一种方式:

let sourceTopic = Object.assign({}, {
        image_info: [],
        video_info: [],
        content: '',
        type: 1
      }, JSON.parse(JSON.stringify(this.$store.getters['topic/signTopic'](topic))))

第二种方式:

let sourceTopic = Object.assign({}, {
        image_info: [],
        video_info: [],
        content: '',
        type: 1
      }, Object.assign({}, this.$store.getters['topic/signTopic'](topic))

我想从状态中使用getter深度复制源对象,该对象在状态列表数组中使用

const getters = {
  /** get sign topic */
  signTopic: (state) => (id) => {
    return state.list.data.find((topic) => topic.id === id) || {}
  }
}

是的,我从状态获得了对象

但是当我更新状态(uploadimage,并将image-url推入状态)时, 第一种方法,永远在源图像数组中只有一个图像URL,

第二种方式,当我对源对象进行Cannel编辑时,上传的图像URL将存储在源对象中

我该如何解决此问题?

0 个答案:

没有答案