Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thunderrun
Last active December 22, 2020 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thunderrun/a675e9b9cef412e1ede87345e9ad6eac to your computer and use it in GitHub Desktop.
Save thunderrun/a675e9b9cef412e1ede87345e9ad6eac to your computer and use it in GitHub Desktop.
Vuex General Purpose Mutations
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
set(state, payload) {
state[payload.key] = payload.value;
},
setVer2(state, payload) {
for (const [key, value] of Object.entries(payload)) {
state[key] = value;
}
},
},
});
// usage
import { mapMutations } from 'vuex'
export default {
// ...
methods: {
...mapMutations(['set', 'setVer2']),
setCount() {
// count => 0
this.set({key: 'count', value: 1});
// count => 1
this.setVer2({count: 2});
// count => 2
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment