Skip to content

Instantly share code, notes, and snippets.

@meshboy
Created September 13, 2018 23:13
Show Gist options
  • Save meshboy/988a1190c2b7dfa11828ffdf8323508d to your computer and use it in GitHub Desktop.
Save meshboy/988a1190c2b7dfa11828ffdf8323508d to your computer and use it in GitHub Desktop.
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const store = new Vuex.Store({
state: {
counter: 0
},
getters: {
/**
* access counter in state from the paramater
*/
addCurrencyToCounter: function (state) {
return `$ ${state.counter} (dollars)`;
},
incrementCounterByTen: function(state) {
return state.counter + 10
}
},
mutations: {
increase: function(state) {
state.counter ++;
},
decrement: function(state) {
state.counter++;
}
},
actions: {
/**
* destruct the context, get the commit and call on the appropriate mutation
*/
increase: function({ commit }) {
commit('increase')
},
decrease: function({ commit }) {
commit('decrement');
},
/**
* demonstrate an async task
*/
asyncIncrement: function({ commit }) {
setTimeout(function(){
/**
* am done, kindly call appropriate mutation
*/
commit('increment')
}, 3000);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment