【发布时间】:2018-05-19 06:11:09
【问题描述】:
如何使用 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 中的反应性与您所期望的不同。在状态对象中预先创建属性或使用 Vue.set 函数 - vuejs.org/v2/api/#Vue-set
-
Vuex Mutations 和 Actions 只能接收两个参数,查看文档vuex.vuejs.org/en/mutations.html
-
使用 vuex 更新数组中对象的任何方法?直接使用 axios 很好,但我对状态管理很满意。
标签: javascript vue.js vuejs2 vuex