将表索引传递到我的vuex存储

时间:2018-07-02 15:00:47

标签: vuejs2 vuex

我正在尝试传递要删除的记录和表的索引,以便可以对其进行更新。

似乎删除工作正常,但不能从表中删除正确的记录。

我已经尝试了一些变体并读了很多书,但仍然不太清楚,所以请寻求指导。

我的组件

//recordID - record to delete from API
// index - index in table
// console log returns correct info for both
onDelete (recordId, index) {
  this.$store.dispatch('cases/deleteCase', recordId, index)
}

// also tried this.$store.dispatch('cases/deleteCase', (recordId, index)) but didn't work or delete

在我的商店中有

动作

deleteCase ({ commit, context }, data, index) {
  console.log(data)
  return new Promise ((resolve, reject) => {
   //Delete works as expected
   this.$axios.delete('/cases/' + data + '.json')
  .then(
   resolve(commit('DELETE_CASE', index))
    )
    .catch(e => {
      context.error(e)
      reject('/cases/')
    })
  })
}, 

我的突变

// delete a todo
DELETE_CASE (state, index) {
  state.cases.splice(index, 1);
}

非常感谢

1 个答案:

答案 0 :(得分:0)

弄清楚了,与其他有类似问题的人分享

需要传递对象

onDelete (recordId, index) {
this.payload = {'recordId': recordId, 'index': index}
 this.$store.dispatch('cases/deleteCase', this.payload)
}

然后我可以在vuex操作中分开

deleteCase ({ commit, context }, payload) {
      return new Promise ((resolve, reject) => {
       this.$axios.delete('/cases/' + payload.recordId + '.json')
      .then(
       resolve(commit('DELETE_CASE', payload.index))
        )
        .catch(e => {
          context.error(e)
          reject('/cases/')
        })
      })
    },