【问题标题】:Passing table index to my vuex store将表索引传递给我的 vuex 存储
【发布时间】:2018-07-02 15:00:47
【问题描述】:

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

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

我尝试了一些变体并阅读了很多,但仍然无法完全弄清楚这一点,所以请寻求指导。

我的组件

//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);
}

非常感谢

【问题讨论】:

    标签: vuejs2 vuex


    【解决方案1】:

    想通了,分享给其他有类似问题的人

    需要传递一个对象

    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/')
            })
          })
        }, 
    

    【讨论】:

      猜你喜欢
      • 2019-11-23
      • 2018-04-17
      • 2018-10-02
      • 2019-04-20
      • 2021-09-07
      • 2019-07-23
      • 2018-11-30
      • 1970-01-01
      • 2012-09-01
      相关资源
      最近更新 更多