使用 vuex 更新数据

新手上路,请多包涵

作为 Vuex,我正在尝试使用表单更新对象。我的代码是这样的。

有存货:

 const state = {
   categories: []
};

//mutations:
[mutationType.UPDATE_CATEGORY] (state, id, category) {
    const record = state.categories.find(element => element.id === id);
    state.categories[record] = category;
}

//actions:
updateCategory({commit}, id, category) {
  categoriesApi.updateCategory(id, category).then((response) => {
    commit(mutationType.UPDATE_CATEGORY, id, response);
    router.push({name: 'categories'});
  })
}

.Vue 文件中的模板:

     <form>
      <div class="form-group">
        <label for="Name">Name</label>
        <input
          type="text"
          class="form-control form-control-sm"
          name="name"
          v-model.lazy="category.name" required>
      </div>

      <div class="form-group">
        <label for="Slug">Slug</label>
        <input
          type="text"
          class="form-control form-control-sm"
          name="slug"
          v-model.lazy="category.slug" required>
      </div>

      <div class="form-group">
        <label for="Avatar">Avatar</label>
        <input
          type="text"
          class="form-control form-control-sm"
          name="avatar"
          v-model.lazy="category.avatar" required>
      </div>

      <div class="form-group">
        <label for="Description">Description</label>
        <textarea
          type="text"
          class="form-control form-control-sm"
          rows="5"
          name="description"
          v-model.lazy="category.description"></textarea>
      </div>

      <div class="form-group">
        <button type="submit" @click.prevent="updateCategory" class="btn btn-outline btn-sm">Update</button>
      </div>

    </form>

在脚本中:

 export default {
    data() {
      return {
        category: {}
      }
    },

    methods: {
      getCategoryById(id) {
        axios.get(`categories/${id}`)
          .then(response => {
            this.category = response.data;
          })
          .catch(error => {
            console.log(error);
          })
      },

      // Using mutation.
      updateCategory() {
        this.$store.dispatch('updateCategory', this.$route.params.id, this.category);
        console.log(this.category); //Display exactly data changed.
      }
    },

    created() {
      this.getCategoryById(this.$route.params.id);
    }
}

我的问题是当我单击更新时。它没有任何改变。我确实在控制台中打印了 category 对象。它显示的正是我所期望的。但是点击更新按钮后。它没有改变。

任何人都可以告诉我为什么并给我解决方案?谢谢。

原文由 Thuan Nguyen 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 372
2 个回答

您可以将参数包装在 1 payload 对象中:

在你的组件中

this.$store.dispatch('updateCategory', {
  id: this.$route.params.id,
  data: this.category
});

在您的商店中,您需要在编辑时创建新对象 categories 数组(您可以阅读有关不可变的更多信息)

 const state = {
   categories: []
};

//mutations:
[mutationType.UPDATE_CATEGORY] (state, payload) {
    state.categories = state.categories.map(category => {
      if (category.id === payload.id) {
        return Object.assign({}, category, payload.data)
      }
      return category
    })
}

//actions:
updateCategory({commit}, payload) {
  categoriesApi.updateCategory(payload.id, payload.data).then((response) => {
    commit(mutationType.UPDATE_CATEGORY, payload);
    router.push({name: 'categories'});
  })
}

原文由 ittus 发布,翻译遵循 CC BY-SA 4.0 许可协议

更简单,只需使用方法修改数组(永远不要直接修改它们)(检查这个: https ://vuejs.org/2016/02/06/common-gotchas/#Why-isn%E2%80%99t- the-DOM-updating 甚至旧的和 DOM 相关的,仍然有效)

所以只要找到你的对象并在你的突变中替换为 splice :

 const index = state.objectsArray.map(o => o.id).indexOf(newObject.id);
state.objectsArray.splice(index, 1, newObject);

原文由 estani 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题