Vue中vuex状态管理mutations用法

<div>{{num}}</div>

<button @click="add">++</button>

 

<script>
import { mapState, mapMutations } from "vuex";
export default {
name: 'HomeIndex',
components:{
SaleCard
},
computed:{
...mapState(['num']),
},
methods:{
add(){
this.$store.commit('madd')
}

 ...mapMutations(["madd"]),

}
}
</script>

const store = new Vuex.Store({

  state:{
     num:22
  },
  mutations:{

    madd(state){
      state.num++
    }

  },

  actions:{

  }

})
posted @ 2025-12-27 14:13  学无边涯  阅读(2)  评论(0)    收藏  举报