Skip to content

Instantly share code, notes, and snippets.

@czbaker
Created May 6, 2018 17:58
Show Gist options
  • Select an option

  • Save czbaker/2860d53cfa2e640d88431a3418ccee57 to your computer and use it in GitHub Desktop.

Select an option

Save czbaker/2860d53cfa2e640d88431a3418ccee57 to your computer and use it in GitHub Desktop.
<template>
<div>
<h2>Vuex Example:</h2>
<button type="button" @click="decrement(5)">Subtract 5</button>
<button type="button">Subtract 1</button>
<input type="text" disabled v-model="counter"/>
<button type="button">Add 1</button>
<button type="button">Add 5</button>
</div>
</template>
<script>
import store from '../store';
import { mapMutations } from 'vuex';
export default {
name: 'vuex',
computed: {
counter() {
return store.state.counter
}
},
methods: {
increment: function (n) {
store.commit('increment', n);
},
decrement: function (n) {
store.commit('decrement', n);
}
}
}
</script>
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
counter: 0
},
mutations: {
increment(state, n) {
state.counter += n
},
decrement(state, n) {
state.counter -= n
},
},
});
export default store;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment