首页 > 其他分享 > vuex中的mapMutations和mapActions

vuex中的mapMutations和mapActions

时间:2022-11-16 10:45:54浏览次数:57  
标签:... 写法 state mapActions mapMutations vuex store

<template>
  <div class="count">
    <h2 > 当前求和为 :{{sum}}</h2>
    <h3 > 当前求和放大十倍为为 :{{bigSum}}</h3>
    <h3 > 我在:{{address}} 学习:{{subject}}</h3>
    <select v-model.number="n">
      <option :value="1">1 </option>
      <option :value="2">2 </option>
      <option :value="3">3 </option>
    </select>
    <button @click="increment(n)">+</button>
    <button @click="decrement(n)">-</button>
    <button @click="incrementOdd(n)">当前求和为奇数再加</button>
    <button @click="incrementWate(n)">等一等再加</button>
  </div>
</template>

<script>
import {mapState, mapGetters,mapMutations,mapActions} from 'vuex'

export default {
    name:"Count",
    data() {
      return {
        n:1,//用户选择的数字
        
      }
    },
    computed:{
      /* 自己写的计算属性 */
      /* sum(){
        return this.$store.state.sum
      },     
      address(){
        //  console.log(this.$store)
        return this.$store.state.address
      },
      subject(){
        return this.$store.state.subject
      }, */
      /* **************************** */
      // 借助mapState生成计算属性,从state中读取数据(对象写法)
      /* ...mapState({
        sum:"sum",
        address:"address",
        subject:"subject",
      }), */
      // 借助mapState生成计算属性,从state中读取数据(数组写法)
      ...mapState(["sum","address","subject"]),

      /* ******************************************* */
      // bigSum(){
      //   return this.$store.getters.bigSum
      // },
      // 借助mapGetters生成计算属性,从state中读取数据(对象写法)
      /* ...mapGetters({
        bigSum:"bigSum",
      }), */
      // 借助mapGetters生成计算属性,从state中读取数据(数组写法)
      
    ...mapGetters(["bigSum"]),

    },
    methods: {
     /*  increment(){
        // this.$store.dispatch('jia',this.n)
        this.$store.commit('JIA',this.n)
      },
      decrement(){
        this.$store.commit('JIAN',this.n)
      }, */

      // 借助mapMutations生成对应的方法,方法中调用commit去联系mutations(对象写法)
      ...mapMutations({
        increment:"JIA",
        decrement:"JIAN",

      }),

       // 借助mapMutations生成对应的方法,方法中调用commit去联系mutations(数组写法)
      //  ...mapMutations(["",""]),需要改成一样的才能用此方法

      /* ****************************** */
      /* incrementOdd(){
       
        // if (this.Sstore.state.sum%2) {
           this.$store.dispatch('jiaOdd',this.n)
        // }
      },
      incrementWate(){
        // setTimeout(() => {
         this.$store.dispatch('jiaWate',this.n)
        // }, 500);
      }, */

      // 借助mapActions生成对应的方法,方法中调用dispatch去联系action(对象写法)
      ...mapActions({
        incrementOdd:"jiaOdd",
        incrementWate:"jiaWate",
      }),

      // 借助mapActions生成对应的方法,方法中调用dispatch去联系action(数组写法)要改成想对应的名字
      
      // ...mapActions([
      //   "",
      //   ""
      // ])
    },
    mounted() {
      // console.log(this.$store) 
    },
    
}
</script>

<style scoped>
   button{
     margin: 5px;
   }
</style>

 

标签:...,写法,state,mapActions,mapMutations,vuex,store
From: https://www.cnblogs.com/xiaobaizitaibai/p/16895073.html

相关文章

  • 聊聊Vuex原理
    背景Vuex是一个专为Vue.js应用程序开发的状态管理模式。Vuex是专门为Vue.js设计的状态管理库,以利用Vue.js的细粒度数据响应机制来进行高效的状态更新。如果你已......
  • vuex中的getters
    当state中的数据需要经过加工后在使用//该文件用于创建vuex中的store//引入vueximportVuefrom'vue'importVuexfrom'vuex'//使用插件Vue.use(Vuex);//猪备ac......
  • VUEX的基本使用之加减案例
    组件count<template><divclass="count"><h2>当前求和为:{{$store.state.sum}}</h2><selectv-model.number="n"><option:value="1">1</option......
  • Vuex 数据持久化(vuex-persistedstate)
    使用conststore=newVuex.Store({modules:{user:{},},getters,actions,//异步mutations,//同步plugins:[createPersistedState({......
  • Vuex 持久化数据更新(vuex-persistedstate)
    在Vuex的使用过程中,会面临数据持久化问题,如:用户数据、菜单数据、必要的信息数据等。遇到问题:改变数据后F5刷新页面,数据不改变使用方式exportdefault{mounted......
  • Vue2.x下的各组件如何传递信息?(不使用Vuex)
    Vue2(选项式)一,父组件向子组件传递数据:介绍:在引用的子组件中定义自定义属性msg与user.可以通过v-bind绑定要发送的数据。  在子组件中使用props接收自定义属性msg......
  • Vuex管理dialog、toast等常见全局性组件状态时唯一性的问题
    前言工作中经常会用到类似于dialog、toast、popover等一些状态提示组件。对于这种全局性的组件,通常会用到vuex来管理组件的信息。这样的好处是代码维护起来更加友好,但......
  • Vue面试题48:你觉得Vuex有什么缺点?(总结自B站up主‘前端杨村长’视频,仅供自用学习)
    体验使用模块:用起来比较繁琐,使用模式也不统一,基本上得不到类型系统的任何支持:conststore=createStore({ modules:{a:moduleA}})store.state.a......
  • 46.使用过vuex和vue-router吗
    使用过,vuex是状态管理工具,它的数据可以被所有的组件获取,方法可以被所有的组件调用;vuex 的内部的运行机制:state提供了数据驱动视图,dispath派发actions执行异步操作,comm......
  • 组件之间的通信2-->vuex状态管理
    在上期,我们讲了父子组件的传递方式,但是,如果我们想知道这些数据从哪里来的话,就需要一层一层找父组件,最后才能找到数据,容易造成Prop逐级透传问题今天,我们将介绍另一......