vuex mutation与mapMutations

Mutation
通俗的理解mutations,里面装着一些改变数据方法的集合,
这是Veux设计很重要的一点,就是把处理数据逻辑方法全部放在mutations里面,使得数据和视图分离。
切记:Vuex中store数据改变的唯一方法就是mutation!

Vuex 中的 mutation 非常类似于事件:
    每个 mutation 都有一个字符串的事件类型 (type)
    和 一个 回调函数 (handler)
    这个回调函数就是实际进行状态更改的地方,并且它会接受 state 作为第一个参数
    可以理解为Vuex 中的 mutation 非常类似于vue中的$emit事件

const store = new Vuex.Store({
  state: {
    count: 1
  },
  mutations: {
    increment (state) {
      // 变更状态
      state.count++
    }
  }
  // 另一种写法
  mutations: {
	increment: {
		handler: () => {
			// 逻辑函数
		},
		// 事件类型
		type: 'increment'
	}
  }
})

store.commit('increment')
在store的mutations中注册一个名为increment的监听及监听触发后的函数体,
当监听到名为'increment'的commit事件时,会触发store中mutations下increment函数

提交载荷(Payload)
mutation的载荷payload是作为store.commit传入的额外参数存在的。

mutations: {
  increment (state, n) {
    state.count += n
  }
}
store.commit('increment', 10)
此处10作为increment的载荷存在

在大多数情况下,载荷应该是一个对象,这样可以包含多个字段并且记录的 mutation 会更易读:
mutations: {
  increment (state, payload) {
    state.count += payload.amount
  }
}

store.commit('increment', {
  amount: 10
})
对象风格的提交方式
提交 mutation 的另一种方式是直接使用包含 type 属性的对象:
store.commit({
  type: 'increment',
  amount: 10
})

当使用对象风格的提交方式,整个对象都作为载荷传给 mutation 函数,因此 handler 保持不变:
mutations: {
  increment (state, payload) {
    state.count += payload.amount
  }
}

Mutation 需遵守 Vue 的响应规则
注意事项:
    最好提前在 store 中初始化好所有所需属性。
    当需要在对象上添加新属性时,
    应该使用 Vue.set(obj, 'newProp', 123),
    或者以新对象替换老对象。
    利用对象展开运算符...,可以这样写:
    state.obj = { ...state.obj, newProp: 123 }

 

使用常量替代 Mutation 事件类型
使用常量替代 mutation 事件类型在各种 Flux 实现中是很常见的模式。
这样可以使 linter 之类的工具发挥作用,同时把这些常量放在单独的文件中可以让你的代码合作者对整个 app 包含的 mutation 一目了然:

// mutation-types.js
export const SOME_MUTATION = 'SOME_MUTATION'

// store.js
import Vuex from 'vuex'
import { SOME_MUTATION } from './mutation-types'

const store = new Vuex.Store({
  state: { ... },
  mutations: {
    // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
    [SOME_MUTATION] (state) {
      // mutate state
    }
  }
})

用不用常量取决于自身
在需要多人协作的大型项目中,使用mutation常量很有帮助。
但如果不喜欢,也可以不使用

Mutation 必须是同步函数

 

mapMutations

在组件中提交 Mutation
可以在组件中使用 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` 也支持载荷:
      'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
    ]),
    ...mapMutations({
      add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
    })
  }
}

mapMutations的参数传递:
...mapMutations([
    'increment' // 将this.increment() 映射为 this.$store.commit('increment', payload)
		// 在事件或方法中直接调用 this.increment({amount: 10 })即可
    ]),

// 调用
<div @click="increment({amount: 10 })">Click me</div>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值