作为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
对象。它显示我所期望的。
但是点击“更新”按钮后。它没有改变。
任何人都可以告诉我为什么并给我解决方案?感谢。
答案 0 :(得分:5)
您可以将参数包装在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'});
})
}
答案 1 :(得分:1)
ES6
更新方式
//mutations UPDATE:
[mutationType.UPDATE_CATEGORY] (state, payload) {
state.categories = [
...state.categories.map(item =>
item.id !== updatedItem.id ? item : {...item, ...updatedItem}
)
]
}
//mutations CREATE:
[mutationType.CREATE_CATEGORY] (state, category) {
state.categories = [category, ...state.categories] //Append to start of array
}
//mutations DELETE:
[mutationType.DELETE_CATEGORY] (state, id) {
state.categories = [
...state.categories.filter((item) => item.id !== id)
];
}
答案 2 :(得分:0)
更简单的是,只需使用方法来修改数组(切勿直接修改它们)(请检查:https://vuejs.org/2016/02/06/common-gotchas/#Why-isn%E2%80%99t-the-DOM-updating即使是旧的并且与DOM相关,仍然有效)
所以只需找到您的对象并在您的突变中替换为拼接:
const index = state.objectsArray.map(o => o.id).indexOf(newObject.id);
state.objectsArray.splice(index, 1, newObject);
答案 3 :(得分:0)
您只需要一个行代码,因为javascript对象具有boolean indexing
//mutations:
[mutationType.UPDATE_CATEGORY] (state, id, category) {
Object.assign(state.categories.find(element => element.id === id), category);
}