vuex用户自行重复而不是自我更新
我有一个用户表和一种负责通过AXIO将其存储在数据库中的方法。
async saveUser({ commit }, user) {
let createdUser;
if (user.id) {
createdUser = await http.put(`/users/${user.id}`, user);
commit('addUser', createdUser.data);
} else {
createdUser = await http.post('/users', user);
commit('addUser', createdUser.data);
}
},
添加一切都很好,但是在编辑用户而不是更新用户时,将添加使用更改的数据的副本。使用旧数据的用户仅在刷新页面时就会消失。
mutations: {
addUser(state, data) {
state.users.unshift(data);
},
}
我想找到一种消除重复的方法,以便立即更新用户
I have a table of users and a method that is responsible for storing them in the database via axios.
async saveUser({ commit }, user) {
let createdUser;
if (user.id) {
createdUser = await http.put(`/users/${user.id}`, user);
commit('addUser', createdUser.data);
} else {
createdUser = await http.post('/users', user);
commit('addUser', createdUser.data);
}
},
Everything works fine for adding, but when editing a user, instead of updating the user, a duplicate is added with the changed data. The user with the old data only disappears when the page is refreshed.
mutations: {
addUser(state, data) {
state.users.unshift(data);
},
}
I would like to find a way to eliminate the duplication so that the user is updated immediately
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的操作方法中,尝试创建一种用于更新的方法。例如,
请确保您在“有效负载”中传递用户ID,然后使用Axios方法更新DB.Collection()。希望你觉得这有用
Inside your actions method try to create a method for updating. For Example
Make sure you pass the user id inside "payload" and update the db.collection() with your axios method. Hope you find this useful