简体   繁体   English

Vuex 商店不更新 state

[英]Vuex store doesn't update the state

My app has a small feature that if a user click on a particular image, it should get removed by a list and moved into another in the Vuex store.我的应用程序有一个小功能,如果用户单击特定图像,它应该被列表删除并移动到 Vuex 商店中的另一个。

It is something really simple:这很简单:

// Action
  async movePicture({ commit }, data) {
    try {
      const comment = await this.$axios.$post('/photo-check', data)
      commit('MOVE_PHOTO', photoId)
    } catch (error) {
      throw error.response
    }
  },

  // Mutation
  MOVE_PHOTO: (state, id) => {
    const i = _.findIndex(state.list, p => p.id === id)
    if (i > -1) {
      const photo = state.list[i]
      state.list.splice(i, 1)
      state.visited.push(photo)
    }
  },

The point is that unfortunately this picture wont splice from the list and I do not understand why... Maybe I should use something specific to update arrays in Vuex and I don't know it... also I can not find any different solution than this...关键是不幸的是这张图片不会从list拼接出来,我不明白为什么......也许我应该使用特定的东西来更新 Vuex 中的 arrays 我不知道......我也找不到任何不同的解决方案比这个...

JavaScript isn't good at handling these sitautions. JavaScript 不擅长处理这些情况。

You should use你应该使用

this.$delete(state.list, index)

Vue.delete does the magic bit for you. Vue.delete为你做了一些神奇的事情。

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

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