Skip to content

Instantly share code, notes, and snippets.

@MaraAlexa
Created January 9, 2018 18:05
Show Gist options
  • Save MaraAlexa/b8c75d78ee9966a7b142a842663ab0f4 to your computer and use it in GitHub Desktop.
Save MaraAlexa/b8c75d78ee9966a7b142a842663ab0f4 to your computer and use it in GitHub Desktop.
// MUTATIONS - Vuex mutations are essentially events: each mutation has a name and a handler - Mutations mutate the state
- is the only way to modify state
- doesn't care about business logic, it just care about "state"
const store = new Vuex.Store({
state: {
count: 1
},
mutations: {
INCREMENT (state) {
// mutate state
state.count++
}
}
})
// ACTIONS - Actions are just functions that dispatch mutations - Actions commit mutations
- action is business logic
- action can dispatch more than 1 mutation at a time
- it just implement the business logic, it doesn't care about data changing (which manage by mutation)
// the simplest action
function increment (store) {
store.dispatch('INCREMENT')
}
// a action with additional arguments - with ES2015 argument destructuring
function incrementBy ({ dispatch }, amount) {
dispatch('INCREMENT', amount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment