核心提示:第一节我们初步见识了Mutations,这节我们再来详细看看,首先我们在store里面的mutations的方法加个参数const mutations = {add(state,n){state.co...
第一节我们初步见识了Mutations,这节我们再来详细看看,首先我们在store里面的mutations的方法加个参数
const mutations = { add(state,n){ state.count+=n; }, reduce(state){ state.count--; } }
然后是我们的Count.vue组件
{{ msg }}
{{ $store.state.count }}--{{ count }}
<script> import store from '@/vuex/store' //这里多了个mapMutations import { mapState,mapMutations } from 'vuex' export default { data(){ return { msg: 'Hello Vuex!' } }, computed:mapState(['count']), //将mutations添加到menthods里面 methods:mapMutations(['add','reduce']), //将store添加到实例 store } </script>
提示:这里我们可以看看我们调用方法时和声明方法的参数是有区别的
add(state,n);//声明 add(10);//调用,state是不用传的 //按我们第一节调用的,也是不一样的 $store.commit('add',10);