总括Vuex的五个核心属性
Vue有五个核心概念,state, getters, mutations, actions, modules。本文将对这个五个核心概念进行梳理。
state => 基本数据 getters => 从基本数据派生的数据 mutations => 提交更改数据的方法,同步! actions => 像一个装饰器,包裹mutations,使之可以异步。 modules => 模块化Vuex
1.state
state即Vuex中的基本数据!
单一状态树
Vuex使用单一状态树,即用一个对象就包含了全部的状态数据。state作为构造器选项,定义了所有我们需要的基本状态参数。
在vue组件中使用 store.state.count 来获取仓库里state的数据
mapState辅助函数
当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键。
// 在单独构建的版本中辅助函数为 Vuex.mapState import { mapState } from 'vuex' export default { // ... computed: mapState({ // 箭头函数可使代码更简练 count: state => state.count, // 传字符串参数 'count' 等同于 `state => state.count` countAlias: 'count', // 为了能够使用 `this` 获取局部状态,必须使用常规函数 countPlusLocalState (state) { return state.count + this.localCount } }) }
当映射的计算属性的名称与 state 的子节点名称相同时,我们也可以给 mapState 传一个字符串数组。
computed: mapState([ // 映射 this.count 为 store.state.count 'count' ])
2.mutations
提交mutation是更改Vuex中的store中的状态的唯一方法。
mutation必须是同步的,如果要异步需要使用action。
每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数,提交载荷作为第二个参数。(提交荷载在大多数情况下应该是一个对象),提交荷载也可以省略的。
const store = new Vuex.Store({ state: { count: 1 }, mutations: { //无提交荷载 increment(state) { state.count++ } //提交荷载 incrementN(state, obj) { state.count += obj.n } } })
你不能直接调用一个 mutation handler。这个选项更像是事件注册:“当触发一个类型为 increment 的 mutation 时,调用此函数。”要唤醒一个 mutation handler,你需要以相应的 type 调用 store.commit 方法:
//无提交荷载 store.commit('increment') //提交荷载 store.commit('incrementN', { n: 100 })
mapMutations 辅助函数
与其他辅助函数类似,你可以在组件中使用 this.$store.commit(‘xxx’) 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。
import { mapMutations } from 'vuex' export default { //.. methods: { ...mapMutations([ 'increment' // 映射 this.increment() 为 this.$store.commit('increment') ]), ...mapMutations({ add: 'increment' // 映射 this.add() 为 this.$store.commit('increment') }) } }
3.actions
Action 类似于 mutation,不同在于:
* Action 提交的是 mutation,而不是直接变更状态。
* Action 可以包含任意异步操作。
我们用如下例子来结束actions:
const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } }, actions: { increment (context) { setInterval(function(){ context.commit('increment') }, 1000) } } })
分发actions
Action 通过 store.dispatch 方法触发:
store.dispatch('increment')
其他与mutations类似的地方
Actions 支持同样的载荷方式和对象方式进行分发:
// 以载荷形式分发 store.dispatch('incrementN', { n: 10 }) // 以对象形式分发 store.dispatch({ type: 'incrementN', n: 10 })
mapActions辅助函数
你在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store):
import { mapActions } from 'vuex' export default { //.. methods: { ...mapActions([ 'incrementN' //映射 this.incrementN() 为 this.$store.dispatch('incrementN') ]), ...mapActions({ add: 'incrementN' //映射 this.add() 为 this.$store.dispatch('incrementN') }) } }
4.getters
即从store的state中派生出的状态。
getters接收state作为其第一个参数,接受其他 getters 作为第二个参数,如不需要,第二个参数可以省略如下例子:
const store = new Vuex.Store({ state: { count:0 }, getters: { // 单个参数 countDouble: function(state){ return state.count * 2 }, // 两个参数 countDoubleAndDouble: function(state, getters) { return getters.countDouble * 2 } } })
在vue组件中使用 $store.getters.countDouble 来获取仓库里getters的数据
5.Modules
使用单一状态树,导致应用的所有状态集中到一个很大的对象。但是,当应用变得很大时,store 对象会变得臃肿不堪。
为了解决以上问题,Vuex 允许我们将 store 分割到模块(module)。每个模块拥有自己的 state、mutation、action、getters、甚至是嵌套子模块——从上至下进行类似的分割:
const moduleA = { state: { ... }, mutations: { ... }, actions: { ... }, getters: { ... } } const moduleB = { state: { ... }, mutations: { ... }, actions: { ... } } const store = new Vuex.Store({ modules: { a: moduleA, b: moduleB } }) store.state.a // -> moduleA 的状态 store.state.b // -> moduleB 的状态
如果要调用moduleA里面的参数:store.state.a.参数名
如果要调用moduleB里面的参数:store.state.b.参数名
如果要调用moduleA里面的 mutations 方法:this.$store.commit("a/方法名");
如果要调用moduleB里面的 mutations 方法:this.$store.commit("b/方法名");
具体模块化后的全局操作:请点击我