Vuex中四个map方法的使用

1.mapState方法:

        用于帮助我们映射state中的数据为计算属性        

2.mapGetters方法:

        用于帮助我们映射getters中的数据为计算属性

<template>
  <div>
    <h1>当前值为: {{sum}}</h1>
    <h2>数值放大{{bigSum}}</h2>
    <List/>
  </div>
</template>

<script>
import List from './components/List.vue'
// 导入mapState, mapGetters方法
import { mapState, mapGetters} from 'vuex'
export default {
  name: 'App',
  components: {
    List
  },
  computed: {
    //借助mapState生成计算属性:sum(对象写法)
    // ...mapState({sum:'sum'}),
    //借助mapState生成计算属性:sum(数组写法)
    ...mapState(['sum']),

    //借助mapGetters生成计算属性:bigSum(对象写法)
    // ...mapGetters({bigSum: 'bigSum'}),
     //借助mapGetters生成计算属性:bigSum(数组写法)
    ...mapGetters(['bigSum'])

  }
}
</script>

3.mapActions方法:

        用于帮助我们生成与actions对话的方法,即:包含$store.dispatch(xxx)的函数

4.mapMutations方法:

        用于帮助我们生成与mutations对话的方法,即:包含$store.commit(xxx)的函数

<template>
  <div>
    <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="awaitAdd(n)">延迟点击+</button>
  </div>
</template>

<script>
// mapActions, mapMutations
import { mapActions, mapMutations } from "vuex";
export default {
  data() {
    return {
      n: 1,
    };
  },
  methods: {
    //亲自写方法
    // add () {
    //   this.$store.commit('add', this.n)
    // },
    // awaitAdd() {
    //   this.$store.dispatch('awaitAdd', this.n)
    // }

    //借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(对象写法)
    // ...mapMutations({add: 'add'}),
    //借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(数组写法)
    ...mapMutations(["add"]),

    //借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(对象写法)
    // ...mapActions({ awaitAdd: "awaitAdd" }),
    //借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(数组写法)
    ...mapActions(["awaitAdd"]),
    
  },
};
</script>

<style>
</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Vue.js使用Vuex进行状态管理时,我们通常会使用map函数来简化代码。map函数提供了一些辅助函数,可以将Vuex的状态映射到组件的计算属性或方法。以下是常用的map函数: 1. mapState:将Vuex的状态映射到组件的计算属性。 ```javascript import { mapState } from 'vuex' export default { computed: { ...mapState({ count: state => state.count }) } } ``` 2. mapGetters:将Vuex的getter映射到组件的计算属性。 ```javascript import { mapGetters } from 'vuex' export default { computed: { ...mapGetters([ 'doneTodosCount', 'anotherGetter' ]) } } ``` 3. mapMutations:将Vuex的mutation映射到组件的方法。 ```javascript import { mapMutations } from 'vuex' export default { methods: { ...mapMutations([ 'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')` 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)` ]), ...mapMutations({ add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')` }) } } ``` 4. mapActions:将Vuex的action映射到组件的方法。 ```javascript import { mapActions } from 'vuex' export default { methods: { ...mapActions([ 'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')` 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)` ]), ...mapActions({ add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')` }) } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李公子丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值