1. mapState方法:

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

computed: {//借助mapState生成计算属性:sum、school、subject(对象写法)...mapState({sum:'sum',school:'school',subject:'subject'}),//借助mapState生成计算属性:sum、school、subject(数组写法)...mapState(['sum','school','subject']),
},

2. mapGetters方法:

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

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

例子:

不用mapState,mapGetters方法,原始写

<template><div><h1>当前求和为:{{$store.state.sum}}</h1><h3>当前求和放大10倍为:{{$store.getters.bigSum}}</h3><h3>我在{{$store.state.school}},学习{{$store.state.subject}}</h3></div>
</template><script>export default {name:'Count',}
</script><style lang="css"></style>

store/index.js

//该文件用于创建Vuex中最为核心的store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)//准备state——用于存储数据
const state = {sum:1, //当前的和school:'尚硅谷',subject:'前端'
}
//准备getters——用于将state中的数据进行加工
const getters = {bigSum(state){return state.sum*10}
}//创建并暴露store
export default new Vuex.Store({state,getters
})

用mapState,mapGetters方法写

count.vue

<template><div><h1>当前求和为:{{sum}}</h1><h3>当前求和放大10倍为:{{bigSum}}</h3><h3>我在{{school}},学习{{subject}}</h3></div>
</template><script>import {mapState,mapGetters} from 'vuex'export default {name:'Count',data() {return {n:1, //用户选择的数字}},computed:{//借助mapState生成计算属性,从state中读取数据。(对象写法)// ...mapState({sum:'sum',school:'school',subject:'subject'}),//借助mapState生成计算属性,从state中读取数据。(数组写法)...mapState(['sum','school','subject']),//借助mapGetters生成计算属性,从getters中读取数据。(对象写法)// ...mapGetters({bigSum:'bigSum'})//借助mapGetters生成计算属性,从getters中读取数据。(数组写法)...mapGetters(['bigSum'])},}
</script><style lang="css"></style>

store/index.js

//该文件用于创建Vuex中最为核心的store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)//准备state——用于存储数据
const state = {sum:1, //当前的和school:'尚硅谷',subject:'前端'
}
//准备getters——用于将state中的数据进行加工
const getters = {bigSum(state){return state.sum*10}
}//创建并暴露store
export default new Vuex.Store({state,getters
})

3. mapActions方法:

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

methods:{//靠mapActions生成:incrementOdd、incrementWait(对象形式)...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})//靠mapActions生成:incrementOdd、incrementWait(数组形式)...mapActions(['jiaOdd','jiaWait'])
}

4. mapMutations方法:

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

methods:{//靠mapActions生成:incrementOdd、incrementWait(对象形式)...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})//靠mapActions生成:incrementOdd、incrementWait(数组形式)...mapActions(['jiaOdd','jiaWait'])
}

例子:

不用mapMutations,mapActions

store/index.js

//该文件用于创建Vuex中最为核心的store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)//准备actions——用于响应组件中的动作
const actions = {jiaOdd(context,value){console.log('actions中的jiaOdd被调用了')if(context.state.sum % 2){context.commit('JIA',value)}},jiaWait(context,value){console.log('actions中的jiaWait被调用了')setTimeout(()=>{context.commit('JIA',value)},500)}
}
//准备mutations——用于操作数据(state)
const mutations = {JIA(state,value){console.log('mutations中的JIA被调用了')state.sum += value},JIAN(state,value){console.log('mutations中的JIAN被调用了')state.sum -= value}
}
//准备state——用于存储数据
const state = {sum:1, //当前的和
}//创建并暴露store
export default new Vuex.Store({actions,mutations,state,
})

count.vue

<template><div><h1>当前求和为:{{$store.state.sum}}</h1><select v-model.number="n"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select><button @click="increment">+</button><button @click="decrement">-</button><button @click="incrementOdd">当前求和为奇数再加</button><button @click="incrementWait">等一等再加</button></div>
</template><script>export default {name:'Count',data() {return {n:1, //用户选择的数字}},methods: {increment(){this.$store.commit('JIA',this.n)},decrement(){this.$store.commit('JIAN',this.n)},incrementOdd(){this.$store.dispatch('jiaOdd',this.n)},incrementWait(){this.$store.dispatch('jiaWait',this.n)},},}
</script><style lang="css">button{margin-left: 5px;}
</style>

用mapMutations,mapActions

<template><div><h1>当前求和为:{{$store.state.sum}}</h1><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="incrementWait(n)">等一等再加</button></div>
</template><script>import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'export default {name:'Count',data() {return {n:1, //用户选择的数字}},methods: {//借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(对象写法)...mapMutations({increment:'JIA',decrement:'JIAN'}),//借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(数组写法)// ...mapMutations(['JIA','JIAN']),/* ************************************************* *///借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(对象写法)...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})//借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(数组写法)// ...mapActions(['jiaOdd','jiaWait'])},}
</script><style lang="css">button{margin-left: 5px;}
</style>

备注:mapActions与mapMutations使用时,若需要传递参数需要:在模板中绑定事件时传递好参数,否则参数是事件对象。

总代码:

count.vue

<template><div><h1>当前求和为:{{sum}}</h1><h3>当前求和放大10倍为:{{bigSum}}</h3><h3>我在{{school}},学习{{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="incrementWait(n)">等一等再加</button></div>
</template><script>import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'export default {name:'Count',data() {return {n:1, //用户选择的数字}},computed:{//借助mapState生成计算属性,从state中读取数据。(数组写法)...mapState(['sum','school','subject']),//借助mapGetters生成计算属性,从getters中读取数据。(数组写法)...mapGetters(['bigSum'])},methods: {//借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(对象写法)...mapMutations({increment:'JIA',decrement:'JIAN'}),//借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(对象写法)...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})},mounted() {const x = mapState({he:'sum',xuexiao:'school',xueke:'subject'})console.log(x)},}
</script><style lang="css">button{margin-left: 5px;}
</style>

store/index.js

//该文件用于创建Vuex中最为核心的store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)//准备actions——用于响应组件中的动作
const actions = {jiaOdd(context,value){console.log('actions中的jiaOdd被调用了')if(context.state.sum % 2){context.commit('JIA',value)}},jiaWait(context,value){console.log('actions中的jiaWait被调用了')setTimeout(()=>{context.commit('JIA',value)},500)}
}//准备mutations——用于操作数据(state)
const mutations = {JIA(state,value){console.log('mutations中的JIA被调用了')state.sum += value},JIAN(state,value){console.log('mutations中的JIAN被调用了')state.sum -= value}
}//准备state——用于存储数据
const state = {sum:0, //当前的和school:'尚硅谷',subject:'前端'
}//准备getters——用于将state中的数据进行加工
const getters = {bigSum(state){return state.sum*10}
}//创建并暴露store
export default new Vuex.Store({actions,mutations,state,getters
})

App.vue

<template><div><Count/></div>
</template><script>import Count from './components/Count'export default {name:'App',components:{Count},}
</script>

main.js

//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//引入插件
import vueResource from 'vue-resource'
//引入store
import store from './store'//关闭Vue的生产提示
Vue.config.productionTip = false
//使用插件
Vue.use(vueResource)//创建vm
new Vue({el:'#app',render: h => h(App),store,beforeCreate() {Vue.prototype.$bus = this}
})


参考:
Vuex模块化和命名空间

[vue] Vuex中四个map方法的使用 mapState mapGetters mapActions mapMutations相关推荐

  1. Vue教程5【vuex】getters,mapState,mapGetters,mapActions,mapMutations,模块化namespace

    vuex 什么时候需要使用? 状态(数据)共享! vuex工作原理 搭建vuex环境 创建文件 src/store/index.js//全局安装[--save生产环境] npm install -g ...

  2. Vue学习记录8,vue脚手架配置代理的两种方式,Github搜索案例,vue-resource,三种插槽,Vuex及搭建Vuex环境,getter和四个map方法的使用, 模块化+命名空间

    目录 vue脚手架配置代理 方法一 方法二 Github搜索案例 UserList.vue UserSearch.vue 效果图 vue-resource 插槽 默认插槽 具名插槽 作用域插槽 Vue ...

  3. Vue项目中background-image属性设置方法

    vue项目中background-image属性设置方法 方式一:直接访问 在vue-cli项目中的放在public目录下的资源会被直接复制,不会经过webpack的打包处理. <span cl ...

  4. Java8中list转map方法总结

    背景 在最近的工作开发之中,慢慢习惯了很多Java8中的Stream的用法,很方便而且也可以并行的去执行这个流,这边去写一下昨天遇到的一个list转map的场景. list转map在Java8中str ...

  5. vue项目中使用postcss-px2rem的方法总结

    vue项目中使用postcss-px2rem的2种方法 在项目中为了屏幕适配,经常会用到rem,postcss-px2rem就是为了让我们直接在将代码中px自动转化成对应的rem的一个插件.(下边的方 ...

  6. Vue项目中设置背景图片方法

    例如css样式 background:url("../../assets/head.jpg"); 1.在data中定义如下: export default { name: 'pro ...

  7. vue项目中引入bootstrap的方法

    vue项目中引入bootstrap?下面本篇文章给大家介绍一下.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 相关教程推荐:<bootstrap教程> 在 vue 项目 ...

  8. 在vue开发中会遇到methods方法里有一个函数嵌套另一个函数,最内层函数this取不到data数据,该怎么解决?

    一,问题 在vue的methods方法中两个函数互相嵌套,最内层函数this取不到data数据 二,原因 this的指向问题 三,解决方法 (1)给最外层函数this重新赋值给一个变量 methods ...

  9. vue项目中关闭eslint的方法

    首先请注意, 本文写于 2019-08-16 10:08:13 当时是Vue 2, 这里说的就是那个时期的事. 网上找来找去, 一堆说啥都有的,说注释这个rule,那个rule,全tm毛用没有. 还有 ...

最新文章

  1. MySQL单机多实例-主主复制
  2. MyBatis传入多个参数的问题
  3. python select模块_Python之select模块解析
  4. 4.5 matlab三维曲面(mesh、fmesh、meshc、meshz、surf、fsurf、surfc、surfl)
  5. 数据中心柴油发电机系统的使用和维护
  6. python输出指定范围素数_怎么用python输出指定范围内的质数?
  7. 【大局观很关键】关于找程序的bug
  8. python给excel文件加密码,并重新生成文件
  9. linux服务器配置与管理_一个十多年的系统管理员,忘了如何管理一台服务器
  10. 【前端】倒计时、秒杀、定时器
  11. 一款快速开单 订单发货收货的在线订货管理软件
  12. Python 结合bat批处理文件 实现密码保管箱
  13. oracle创建用户ORA-01045:user lacks CREATE SESSION privilege;logon denied..的问题
  14. C语言1.打印各种三角形
  15. 案例征集 | 2021中国数据资产管理工具市场研究报告
  16. 《测绘综合能力》真题易错本
  17. 联想拯救者y7000电池耗电快_联想拯救者Y7000游戏本测评之温度、续航双测评
  18. 使用pypcd读取pcd时ValueError: field ‘__0000‘ occurs more than once错误
  19. 苹果CMS、海洋CMS自动定时采集-可采集任意自定义指定资源
  20. 学Android必须懂的

热门文章

  1. 机器视觉领域的牛人们的博客
  2. 解决Google和kaggle注册问题以及GitHub上项目的学习
  3. TIA Portal面向对象编程入门
  4. mysql temporary table
  5. Mac下的spotlight无法搜索本地资源的解决办法
  6. 大数据课程体系-学习笔记概要
  7. 结构体类型的变量的初始化
  8. 极大似然估计原理详细说明
  9. 二维数组的转置,并将其按列和行从小到大排列(Java实现)
  10. 如何构建自己的知识体系