Created
August 23, 2016 18:01
-
-
Save yyx990803/4724fa2a66a17e2999c2c360e89f4d9a to your computer and use it in GitHub Desktop.
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
<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