新手教学Vuex(二)

菜鸟教学vuex(二)之 mapState获取vuex的state对象中属性、mapMutations的传参和mapGetters取值

回顾上文: https://blog.****.net/MtangEr/article/details/84992232
上文中提到了
1、获取state中的状态值:

 {{this.$store.state.name}}
或者 : computed 计算属性返回状态值 

2、 store.commit() 方法

一个参数 触发状态变更 : store.commit("addAge")     //addAge 必须是mutations里边的属性值
两个参数   其中第二个参数传参赋值: store.commit("set_name",“mjs”)

今天学习 另外一种获取state属性值、传参修改mutations属性值、getters取值 的方法

1.利用vuex的mapState方法获取vuex的state对象中属性

它有两种写法:

首先在组件中引入vuex的mapState方法:

import { mapState } from ‘vuex’
然后在 computed 中进行操作!
写法1(mapState中用对象的形式获取):
写法2(mapState中用数组的形式获取):
新手教学Vuex(二)
效果
新手教学Vuex(二)

2.利用vuex的mapMutations方法触发状态或传参修改状态值

首先在组件中引入vuex的mapMutations方法:

import { mapMutations } from 'vuex'

然后在 methods 中进行操作!
新手教学Vuex(二)
效果
新手教学Vuex(二)新手教学Vuex(二)

3.利用vuex的mapGetters方法取值

首先在组件中引入vuex的mapMutations方法:

import { mapGetters} from 'vuex'

然后在 computed 中进行操作!
新手教学Vuex(二)

ok 结束了!