vuex mutations、mapMutations、...mapMutations(四)

目录

 mutations 介绍

提交载荷(Payload)

mutation 对象风格提交方式

Mutation 需遵守 Vue 的响应规则

使用常量替代 Mutation 事件类型

Mutation 必须是同步函数

在组件中提交 Mutation

Vuex 案例实现递增递减功能


  • 只能通过 commit mutation 来修改 state 中的状态,如 this.$store.commit('事件名', 传入的额外参数可选)
  • mutation 类似事件,每个 mutation 都有一个字符串的事件类型 type 和一个回调函数 handler
  • mutation 里面定义的事件,接收 state 作为它的第一个参数
  • mutation 提交载荷方式可以向 this.$store.commit() 传入额外的参数,多数情况下载荷是一个对象
  • mutation 对象风格的提交方式,直接使用包含 type 属性的对象
  • mutation 必须是同步函数,方便调试有利于跟踪状态

 mutations 介绍

  • 更改 Vuex store 状态唯一方法是提交 commit mutation
  • Vuex 中的 mutation 非常类似事件:每个 mutation 都有一个字符串的事件类型 (type) 和 一个回调函数 (handler)
  • 这个回调函数是我们实际进行状态更改的地方,并且它接受 state 作为第一个参数
import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 1
  },
  mutations: {
    increment (state) {
      state.count++ // 变更状态
    }
  }
})
  • 不能直接调用 mutation handler 这个选项
  • 它更像事件注册:当触发一个类型为 increment 的 mutation 时,调用此函数
  • 要唤醒一个 mutation handler,需要以相应的 type 调用 this.$store.commit 方法
this.$store.commit('increment')

提交载荷(Payload)

  • 可以向 this.$store.commit() 传入额外的参数,即 mutation 的载荷(payload)
export default {
  mutations: {
    increment (state, n) {
      state.count += n
    }
  }
}
this.$store.commit('increment', 10)
  • 多数情况下,载荷应该是一个对象,这样可以包含多个字段并且记录 mutation 会更易读
export default {
  mutations: {
    increment (state, payload) {
      state.count += payload.amount
    }
  }
}
this.$store.commit('increment', {
  amount: 10
})

mutation 对象风格提交方式

  • 提交 mutation 的另一种方式是直接使用包含 type 属性的对象
this.$store.commit({
  type: 'increment',
  amount: 10
})
  • 当使用对象风格提交方式,整个对象都作为载荷传给 mutation 函数,因此 handler 保持不变
mutations: {
  increment (state, payload) {
    state.count += payload.amount
  }
}

Mutation 需遵守 Vue 的响应规则

既然 Vuex store 中的状态是响应式的,那么当我们变更状态时,监视状态的 Vue 组件也会自动更新。这也意味着 Vuex 中的 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 必须是同步函数。为什么?请参考下面的例子:
mutations: {
  someMutation (state) {
    api.callAsyncMethod(() => {
      state.count++
    })
  }
}

现在想象,我们正在 debug 一个 app 并且观察 devtool 中的 mutation 日志。每一条 mutation 被记录,devtools 都需要捕捉到前一状态和后一状态的快照。然而,在上面例子中 mutation 中的异步函数中的回调让这不可能完成:因为当 mutation 触发的时候,回调函数还没有被调用,devtools 不知道什么时候回调函数实际上被调用——实质上任何在回调函数中进行状态的改变都是不可追踪的。

在组件中提交 Mutation

你可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为  this.$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')`
    })
  }
}

在 mutation 中混合异步调用会导致你的程序很难调试。例如,当你调用了两个包含异步回调的 mutation 来改变状态,你怎么知道什么时候回调和哪个先回调呢?这就是为什么我们要区分这两个概念。在 Vuex 中,mutation 都是同步事务

this.$store.commit('increment')
// 任何由 "increment" 导致的状态变更都应该在此刻完成。

异步操作使用 Action

Vuex 案例实现递增递减功能

  •  页面代码
<template>
  <section>
    {{count}}
    <el-button @click="reduce">-</el-button>
    <el-button @click="plus">+</el-button>
  </section>
</template>

<script>
import { mapMutations, mapState } from "vuex";

export default({
  computed: {
    ...mapState({
      count: state => state.count
    })
  },
  methods: {
    ...mapMutations({
      decrease: 'decrease', // 递减 this.decrease 映射为 this.$store.commit('decrease')
      increase: 'increase', // 递增
    }),
    reduce(){
      this.decrease({
        amount: 1
      })
    },
    plus(){
      this.increase({
        amount: 1
      })
    }
  }
})
</script>
  • store/index.ts 代码
import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'

Vue.use(Vuex)

export default new Vuex.Store({
  state,
  mutations,
})
  • store/state.ts 代码
export default {
  count: 1
}
  • store/mutations.ts 代码
export default {
  // 递减
  decrease(state: any, payload: any){
    state.count -= payload.amount
  },
  // 递增
  increase(state: any, payload: any){
    state.count += payload.amount
  },
}
  • devtools 跟踪的是 mutations 中的状态

 Vuex 状态管理模式(一)_taoqidejingling的博客-CSDN博客_vuex状态管理模式Vuex响应式状态管理模式;Vuex安装和使用;Vuex核心;Vuex优势和缺点;https://blog.csdn.net/taoqidejingling/article/details/121221646


Vuex state、mapState、...mapState (二)_taoqidejingling的博客-CSDN博客使用 computed 接收 state 返回的数据,有 4 种方式;子组件可以通过 this.$store.state 访问 store 状态中的值;mapState 辅助函数帮助我们生成计算属性,可以传入一个对象、也可以传入一个数组;对象展开运算符 ...mapState 函数返回的是一个对象,使用工具函数将多个对象合并为一个对象,最终把对象传给 computed 属性;https://blog.csdn.net/taoqidejingling/article/details/121298272


vuex getters、mapGetters、...mapGetters(三)_taoqidejingling的博客-CSDN博客getter 接收 state 作为他的第一个参数;访问 getter 中的数据有 2 中方式;https://blog.csdn.net/taoqidejingling/article/details/121311301


vuex mutations、mapMutations、...mapMutations(四)_taoqidejingling的博客-CSDN博客只能通过 commit mutation 来修改 state 中的状态,this.$store.commit('事件名', 传入的额外参数可选);mutation 类似事件,每个 mutation 都有一个字符串的事件类型 type 和一个回调函 handler;mutation 接收 state 作为它的第一个参数;mutation提交载荷方式可以向 this.$store.commit() 传入额外的参数,多数情况下载荷是一个对象;mutation 对象风格的提交方式,直接使用包含type属性的对https://blog.csdn.net/taoqidejingling/article/details/121325206

vuex action、mapActions、...mapActions(五)_taoqidejingling的博客-CSDN博客action 和 mutation 的区别?Actions 支持同样的载荷方式和对象方式进行分发,​在组件中分发 ActionAction 类似于 mutation,不同在于:Action 提交的是 mutation,而不是直接变更状态。Action 可以包含任意异步操作。注册一个简单的 action:const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (st.https://blog.csdn.net/taoqidejingling/article/details/121325999

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值