VUEX mapActions 和 mapMutations

 

不使用mapActions 和 mapMutations的代码

要用的地方字体放大了

复制代码
<template>
  <div id="app">
         <h1>当前总数为:{{nbr}}</h1>
         <h2>放大十倍总数为:{{bigSum}}</h2>
         <h3>我在{{school}}我是{{myname}}</h3>
          <select  v-model.number="n">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          </select>
      <button @click="add">+</button>
      <button @click="subtract">-</button>
      <button @click="odd">为奇数再加</button>
      <button @click="item">等一等再加</button>
    <!-- <router-view/> -->
  </div>
</template>
<script>
    import {mapState,mapGetters} from 'vuex';
    export default{
        data(){
            return{
                n:1,
            }
        },
        computed:{
             ...mapState(['nbr','school','myname']),
             // 数组写法 从getters里读取对象
             ...mapGetters(['bigSum'])                  
        },
          methods:{
            add(){
                    // 调用add方法
                this.$store.dispatch('add',this.n)
            },
            subtract(){
                this.$store.dispatch('subtract',this.n)
            },
            odd(){
                  this.$store.dispatch('odd',this.n)
            },
            item(){
            this.$store.dispatch('item',this.n)
            }
        },
    }
</script>
<style lang="less" scoped>
    button{
        margin-left: 5px;
    }
</style>
复制代码

mapMutations:

使用后:给事件加上参数n

复制代码
<template>
  <div id="app">
         <h1>当前总数为:{{nbr}}</h1>
         <h2>放大十倍总数为:{{bigSum}}</h2>
         <h3>我在{{school}}我是{{myname}}</h3>
          <select  v-model.number="n">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          </select>
      <button @click="add(n)">+</button>
      <button @click="subtract(n)">-</button>
      <button @click="odd">为奇数再加</button>
      <button @click="item">等一等再加</button>
    <!-- <router-view/> -->
  </div>
</template>
<script>
    import {mapState,mapGetters,mapMutations} from 'vuex';
    
    export default{
        data(){
            return{
                n:1,
            }
        },
        computed:{
              ...mapState(['nbr','school','myname']),
             // 数组写法 从getters里读取对象
             ...mapGetters(['bigSum'])
             
        },
         methods:{
        //借助mapMutations生成对应的方法,方法中会调用commit去联系mutations ...mapMutations({add:'add',subtract:'subtract'}),
odd(){ this.$store.dispatch('odd',this.n) }, item(){ this.$store.dispatch('item',this.n) } }, } </script> <style lang="less" scoped> button{ margin-left: 5px; } </style>
复制代码

mapActions:

记得给事件加参数n
 <button @click="add(n)">+</button>
      <button @click="subtract(n)">-</button>
      <button @click="odd(n)">为奇数再加</button>
      <button @click="item(n)">等一等再加</button>

 

复制代码
    methods:{            
            // odd(){
                
            //     this.$store.dispatch('odd',this.n)
            // },
            // item(){
            // this.$store.dispatch('item',this.n)
            // }
         //对象写法
...mapActions({odd:'odd',item:'item'}),
          //数组写法
        ...mapActions({'odd','item'}),
},
复制代码

 

 

 

store.js文件

复制代码
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  // 用于存储数据
  state: {
      nbr:0,//当前的和
      school:'京海学校',
      myname:'张三'
  },
  // 用于操作数据
  mutations: {
      //
      add(state,value){
        state.nbr +=value
      },
      //
      subtract(state,value){
              state.nbr -=value
      },
      // 是奇数再加
      odd(state,value){
            if (state.nbr % 2 ==1) {
                state.nbr += value
            }
      },
      // 等一等再加
      item(state,value){
            setTimeout(()=>{
                            state.nbr += value
            //                 console.log(this.nbr);
            //             // 时间间隔
            },1000)
        },
  },
  // 用于响应组件中的动作
  actions: {
      add (context,value){
          //context.commit('名字不是固定的可以和主页面的名字不一样',value)
          context.commit('add',value)
      },
      subtract (context,value){
                context.commit('subtract',value)
      },
      odd(context,value){
                context.commit('odd',value)
      },
      item(context,value){
                context.commit('item',value)
      }
  },
  getters:{
      bigSum(state){
          return state.nbr*10
      }
  }
})
复制代码

 

mapActions
posted @   罗砂  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示