Last active
December 22, 2020 06:54
-
-
Save thunderrun/a675e9b9cef412e1ede87345e9ad6eac to your computer and use it in GitHub Desktop.
Vuex General Purpose Mutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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