简单的vuex小demo
计数小案例
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count: 0
},
mutations: {
add: state => state.count++,
dec: state => state.count--
},
actions: {
}
})
home.vue
<template>
<div class="home">
{{count}}
<button @click="add">+</button>
<button @click="dec">-</button>
</div>
</template>
<script>
// @ is an alias to /src
export default {
name: 'home',
computed:{
count(){
return this.$store.state.count
}
},
methods:{
add(){
this.$store.commit('add')
},
dec(){
this.$store.commit('dec')
}
}
}
</script>