【问题标题】:Vuex mutation inside array of objects after backend update saves后端更新保存后对象数组内的Vuex突变
【发布时间】:2019-03-23 17:29:29
【问题描述】:

我有一个对象数组。当我的 api 执行更新方法并保存时,我正在通过 laravel-echo-server 广播一个事件并尝试使用更新对象改变状态。我正在尝试实时更新。除了实际的突变之外,一切都在按计划进行。这是它的开始:

updateExample (state, example) {
  state.examples.map(e => {
    if (e.id === example.id) {
      // entirely replace the old object with the new
    }
    return false
  })
},

什么是理想的方法?我也可以将旧对象从数组中弹出并推送一个新对象,但这似乎很不稳定。

【问题讨论】:

    标签: vuejs2 state vuex mutation vuex-modules


    【解决方案1】:

    没关系,想通了:

    updateExample (state, example) {
      let index = state.examples.findIndex(e => e.id === example.id)
      if (index !== -1) {
        state.examples[index] = new Example(example)
        return
      }
      state.examples.push(new Example(example))
    }
    

    感谢观看!

    【讨论】:

      猜你喜欢
      • 2019-09-27
      • 1970-01-01
      • 2020-06-01
      • 2019-11-30
      • 1970-01-01
      • 2018-02-04
      • 2019-01-01
      • 2020-02-03
      • 1970-01-01
      相关资源
      最近更新 更多