Skip to content

Instantly share code, notes, and snippets.

@yyx990803
Created August 23, 2016 18:01
Show Gist options
  • Save yyx990803/4724fa2a66a17e2999c2c360e89f4d9a to your computer and use it in GitHub Desktop.
Save yyx990803/4724fa2a66a17e2999c2c360e89f4d9a to your computer and use it in GitHub Desktop.
<script src="https://npmcdn.com/vue@next/dist/vue.js"></script>
<script src="https://npmcdn.com/vuex@next"></script>
<div id="app">
{{ count }}
<button @click="increment"> + </button>
<button @click="decrement"> - </button>
</div>
<script>
const counterModule = {
state: {
count: 1
},
mutations: {
increment (state) {
state.count++
},
decrement (state) {
state.count--
}
},
actions: {
incrementAsync (store) {
setTimeout(function () {
store.commit('increment')
}, 1000)
}
}
}
const store = new Vuex.Store({
strict: true,
modules: {
counter: counterModule
}
})
const app = new Vue({
computed: {
count () {
return store.state.counter.count
}
},
methods: {
increment () {
store.commit('increment')
},
decrement () {
store.commit('decrement')
},
incrementAsync () {
store.dispatch('incrementAsync')
}
}
}).$mount('#app')
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment