Vue中vuex状态管理actions用法

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

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

 

<script>
import SaleCard from "../../components/SaleCard.vue";
import { mapState,mapActions } from 'vuex'
export default {
name: 'HomeIndex',
components:{
SaleCard
},
computed:{
...mapState(['num']),
},
methods:{
add(){
this.$store.dispatch('addAsync',5)
}
...mapActions(["addAsync"])
}
}
</script>

 

  actions:{

    addAsync({commit},n){
       commit('madd',n)
    }

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