辅助函数——vue状态管理(二)

前言

vue的应用状态管理提供了mapState、mapGetters、mapMutations、mapActions四个辅助函数,所谓的辅助函数分别对State、Getters、Mutations、Actions在完成状态的使用进行简化。

mapState

当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性。不使用mapState时,获取对象状态,通常放在使用组件的computes属性中,使用方式为:

  //....
  computed: {
        count: function(){
            return this.$store.state.count
        }
    }
 //....    

使用mapState可以简化为:

import { mapState } from 'vuex'  //引入mapState对象 
export default {
  // ...
  computed: mapState({
    // 箭头函数可使代码更简练
    count: state => state.count,
  })
}
或者
import { mapState } from 'vuex'  //引入mapState对象 
export default {
  // ...
  computed: mapState({
    'count', //与state名称一致
     countAlias:'count' //countAlias是在引用组件中使用的别名
  })
}

mapGetters

mapGetters 辅助函数仅仅是将 store 中的 getters 映射到局部计算属性,与state类似。由计算函数代码简化为;

import { mapGetters } from 'vuex'

export default {
  // ...
  computed: {
  // 使用对象展开运算符将 getters 混入 computed 对象中
    ...mapGetters([
      'countDouble',
      'CountDoubleAndDouble',
      //..
    ])
  }
}

mapGetters也可以使用别名。

mapMutations

使用 mapMutations 辅助函数将组件中的methods映射为store.commit调用,简化后代码为:

import { mapMutations } from 'vuex'

export default {
  //..
  methods: {
    ...mapMutations([
      'increment' // 映射 this.increment() 为 this.$store.commit('increment')
    ]),
    ...mapMutations({
      add: 'increment' // 映射 this.add() 为 this.$store.commit('increment')
    })
  }
}

mapActions

使用 mapActions 辅助函数将组件的methods映射为store.dispatch调用,简化后代码为:

import { mapActions } from 'vuex'

export default {
  //..
  methods: {
    ...mapActions([
      'incrementN' //映射 this.incrementN() 为 this.$store.dispatch('incrementN')
    ]),
    ...mapActions({
      add: 'incrementN' //映射 this.add() 为 this.$store.dispatch('incrementN')
    })
  }
}

示例

沿用vue状态管理(二)中的示例,使用辅助函数完成。在CountChange和ComputeShow两个组件使用了辅助函数,其余代码无需改动。
在ComputeShow使用了mapState,mapGetters两个辅助函数,代码如下

<template>
  <div align="center" style="background-color: bisque;">
    <p>以下是使用computed直接获取stores中的状态数据,状态数据发生变化时,同步刷新</p>
    <h1>使用computed接收 state:{{ computedState }}</h1>
    <h1>使用computed接收Getters:{{ computedGetters }}</h1>
  </div>
</template>

<script>
  import { mapState,mapGetters } from 'vuex'  //引入mapState,mapGetters对象
  export default {
    name: 'ComputeShow',
    computed:{
    ...mapState({
      computedState:'count'  //别名:computedState
    }),

    ...mapGetters({
      computedGetters:'getChangeValue' //别名:computedGetters
    })
    }
  }
</script>

<style>
</style>

建议使用map时,增加别名,这样就和stores里面内容脱耦。在CountChange使用了mapMutations和mapActions两个辅助函数,代码如下

<template>
  <div align="center">
    <input type="number" v-model="paramValue" />
    <button @click="addNum({res: parseInt(paramValue)})">+增加</button>
    <button @click="subNum">-减少</button>
  </div>
</template>

<script>
  import {
    mapMutations,
    mapActions
  } from 'vuex' //引入mapMutations、mapActions对象
  export default {
    name: 'CountChange',
    data() {
      return {
        paramValue: 1,
      }
    },

    methods: {
      ...mapMutations({
        subNum: 'sub'  //增加别名subNum
      }),

      ...mapActions({
        addNum: 'increment' //映射 this.incrementN() 为 this.$store.dispatch('incrementN')
      })
    }
  }
</script>

<style>
</style>

同样给stores中的方法制定别名,当需要传参数时,通过别名将参数传递给actions或mutations。如:"addNum({res: parseInt(paramValue)})"中传送了一个对象{res:‘’}

小结

辅助函数本身没有新的含义,主要用于简化State、Getters、Mutations、Actions使用时的代码。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值