快速学会 Vuex 中的 Map 映射方法

映射方法

映射方法可以让我们使用 Vuex 的方法是更加简洁会减少大量的代码
!!!mutations 和 actions 需要映射到 methods 里,而 state 和 getters 则需要映射到 computed 里

一、普通映射

export default new Vuex.Store({
  state: {
    str: "我是映射过去的 state 方法"
  },
  mutations: {
    fun1(){
      console.log("我是映射过去的 mutations 方法")
    }
  },
  actions: {
    fun2() {
      console.log("我是映射过去的 actions 方法")
    }
  },
  getters: {
    haha() {
      return "我是映射过去的 getters 方法"
    }
  },
  modules: {}
})

调用方法

<template>
  <div id="app">
      {{str}} -------- {{haha}} 
      <br>
      <button @click="fun1">mutations</button>
      <button @click="fun2">actions</button>
  </div>
</template>

<script>
//	引入各种映射方法
import {mapState,mapMutations,mapActions,mapGetters} from "vuex"
export default {
  name: "App",
  methods: {
  	//	用扩展用算符把需要的方法用下面的语法扩展出来
    ...mapMutations(["fun1"]),
    ...mapActions(["fun2"])
  },
  computed:{
 	//	用扩展用算符把需要的方法用下面的语法扩展出来
    ...mapState(["str"]),
    ...mapGetters(["haha"])
  }
};
</script>

二、映射 modules 中的方法

export default new Vuex.Store({
  state: {},
  mutations: {},
  actions: {},
  getters: {},
  modules: {
    mk1:{
      namespaced: true,
      state: {
        str: "我是映射过去的 state 方法"
      },
      mutations: {
        fun1(){
          console.log("我是映射过去的 mutations 方法")
        }
      },
      actions: {
        fun2() {
          console.log("我是映射过去的 actions 方法")
        }
      },
      getters: {
        haha() {
          return "我是映射过去的 getters 方法"
        }
      },
    }
  }
})

使用映射 modules 中的方法

<template>
  <div id="app">
      {{str}} -------- {{haha}} 
      <br>
      <button @click="fun1">mutations</button>
      <button @click="fun2">actions</button>
  </div>
</template>

<script>
import {mapState,mapMutations,mapActions,mapGetters} from "vuex"
export default {
  name: "App",
  methods: {
  	//	只需要在原来写法的基础上加一个路径写法如下
    ...mapMutations('mk1',["fun1"]),
    ...mapActions('mk1',["fun2"])
  },
  computed:{
  	//	只需要在原来写法的基础上加一个路径写法如下
    ...mapState('mk1',["str"]),
    ...mapGetters('mk1',["haha"])
  }
};
</script>

三、普通映射方法传参

export default new Vuex.Store({
  state: {
    str: "我是映射过去的 state 方法"
  },
  mutations: {
    fun1(state,can){
      console.log("我是映射过去的 actions 方法")
      console.log(can)
    }
  },
  actions: {
    fun2(state,can) {
      console.log("我是映射过去的 actions 方法")
      console.log(can)
    }
  },
  getters: {
    haha() {
      return "我是映射过去的 getters 方法"
    }
  },
  modules: {}
})

使用普通映射方法传参

<template>
  <div id="app">
      <button @click="fun1('mutations')">mutations</button>
      <button @click="fun2('actions')">actions</button>
  </div>
</template>

<script>
import {mapState,mapMutations,mapActions,mapGetters} from "vuex"
export default {
  name: "App",
  methods: {
  	//	映射方法传参需要写成对象格式才可传参
    ...mapMutations({
      fun1:"fun1",
    }),
    ...mapActions({
      fun2:"fun2"
    })
  }
};
</script>

四、映射 modules 中的方法传参


export default new Vuex.Store({
  state: {},
  mutations: {},
  actions: {},
  getters: {},
  modules: {
    mk1:{
      namespaced: true,
      state: {
        str: "我是映射过去的 state 方法"
      },
      mutations: {
        fun1(state,can){
          console.log("我是映射过去的 actions 方法")
          console.log(can)
        }
      },
      actions: {
        fun2(state,can) {
          console.log("我是映射过去的 actions 方法")
          console.log(can)
        }
      },
      getters: {
        haha() {
          return "我是映射过去的 getters 方法"
        }
      }
    }
  }
})

使用映射 modules 中的方法传参

<template>
  <div id="app">
      <!-- {{str}} -------- {{haha}}  -->
      <br>
      <button @click="fun1('mutations')">mutations</button>
      <button @click="fun2('actions')">actions</button>
  </div>
</template>

<script>
import {mapState,mapMutations,mapActions,mapGetters} from "vuex"
export default {
  name: "App",
  methods: {
    ...mapMutations('mk1',{
      fun1:"fun1",
    }),
    ...mapActions('mk1',{
      fun2:"fun2"
    })
  }
};
</script>

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

星梦之羽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值