如何在组件内部提交数据给vuex

方法一:
在组件内部事件触发时,通过dispatch传参
methods:{
   handleAddCount(){
       //第一种
       //在组件内部提交数据,以载荷的形式分发
          this.$store.dispatch('increment',{
           amount:10,
       }) //increment 是store/index.js中actions中声明的方法
       //第二种
       this.$store.dispatch({
           type: 'increment',
           amount:10,
       })
   },
}
在store/index.js中的actions中获取参数
actions: { //声明异步的方法
  increment({commit}, {amount}){
    commit('addCount',amount)
  }
}
在store/index.js中的mutations中的声明的方法中获取
addCount(stare,amount){
  stare.count+=amount;
  console.log(amount)
},

 

posted @ 2021-06-10 19:16  是娜娜呀~  阅读(303)  评论(0)    收藏  举报