Skip to content

Instantly share code, notes, and snippets.

@SegersIan
Created September 14, 2017 15:41
Show Gist options
  • Save SegersIan/f09493176f46883dca84b64593680e5d to your computer and use it in GitHub Desktop.
Save SegersIan/f09493176f46883dca84b64593680e5d to your computer and use it in GitHub Desktop.
How to handle updates on a single item in an array located in the Vuex store.
import vuex from 'vuex';
import vue from 'vue';
import * as api from './api';
vue.use(vuex);
export default new vuex.Store({
state: {
users: []
},
mutations: {
setUsers: (state, users) => state.users = users
},
actions: {
fetchUsers({commit}) {
return api.getUsers().then(payload => commit('setUsers', payload));
}
}
});
<template>
<div>
<div class="user">
<div>Id</div>
<div>Name</div>
<div>Enabled</div>
</div>
<div v-for="user in users" class="user">
<div>{{user.id}}</div>
<div>{{user.name}}</div>
<div><input type="checkbox" :value="user.enabled" @change="updateUserStatus"></div>
</div>
</div>
</template>
<script>
import {mapGetters, mapActions, mapState} from 'vuex';
export default {
name: 'Users-with-value-input',
computed: {
...mapState({
// Simple array
users: (state) => state.users
})
},
mounted() {
this.fetchUsers();
},
methods: {
...mapActions(['fetchUsers']),
updateUserStatus(e) {
// Not sure how this is the best way to edit the specific user in a list
//this.$store.commit('updateUserStatus', e.target.value)
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment