Vuex入门(4)—— 用Mutation管理状态
更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。
关于vuex为什么这样做,先不要管,但请相信他这么做必然有很多的好处.在vue中,我们要修改data中的值,一般会怎么做.
this.count = 2 //count from 1 to 2,触发视图更新
很简单,直接赋值,这也符合我们写代码的"一般规律",很舒服.但如果我们要修改vuex store中的状态值,我们就不能简单的通过赋值的方式来做了,如果你这样做,控制台便会报错.
this.$store.state.count = 2 //控制台打印错误
当然vuex的state是可以更改的,不然就太睿智了,vuex提供了mutation来追踪你对state的值的操作,这肯定有什么好处在里面,暂时先把为什么放一边,先了解一下mutation的用法.
Vuex 中的 mutation 非常类似于vue中的$emit事件,
每个 mutation 都有一个字符串的事件类型 (type) ,相当于当前事件的唯一标识,以便于你用commit触发它.
每个mutation都有一个回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数.同时他也支持额外参数的传入,额外参数的术语叫'载荷'.
直接看代码和注释吧,不想过多解释这个
//state.js
let state = {
count: 1,
name: 'dkr',
}
export default state
//mutation.js
// 第一个参数默认接收state对象
const increment = (state) => {
state.count++
}
const decrement = (state) => {
state.count--
}
//第二个参数接收'载荷'
const add = (state, n) => {
state.count += n
}
const fn = (state, json) => {
state.name = json.first + json.second + state.name
}
export {increment, decrement, add, fn}
<template>
<div>
<div>
<button @click="decrement">-</button>
<span>{{count}}</span>
<button @click="increment">+</button>
</div>
<div style="margin-top:20px;">
<button @click="add(1)">+1</button>
<button @click="add(2)">+2</button>
</div>
<button style="margin-top:20px" @click = "changeName('my ','name is ')">{{name}}</button>
</div>
</template>
<script>
export default {
computed: {
count () {
return this.$store.state.count
},
name () {
return this.$store.state.name
}
},
methods: {
decrement () {
this.$store.commit('decrement')
},
increment () {
this.$store.commit('increment')
},
add (n) {
// 你可以向 store.commit 传入额外的参数,即 mutation 的 载荷(payload)
this.$store.commit('add', n)
},
changeName (first, second) {
// this.$store.commit('fn', {
// 'first': first,
// 'second': second
// })
// 上面的写法等同于下面,对象风格的提交方式,个人觉得上面的写法更清晰
this.$store.commit({
'type': 'fn',
'first': first,
'second': second
})
}
}
}
</script>
结果如下:
.png)
关于mutation的辅助函数,我这里提供一下上述代码的辅助函数写法,具体原理请看本系列第二篇文章,不想过多赘述.
<template>
<div>
<div>
<button @click="decrement">-</button>
<span>{{count}}</span>
<button @click="increment">+</button>
</div>
<div style="margin-top:20px;">
<button @click="add(1)">+1</button>
<button @click="add(2)">+2</button>
</div>
<button style="margin-top:20px" @click = "changeName({'first':'my ',second:'name is '})">{{name}}</button>
</div>
</template>
<script>
import { mapMutations } from 'vuex'
export default {
computed: {
count () {
return this.$store.state.count
},
name () {
return this.$store.state.name
}
},
// 辅助函数写法
methods: {
...mapMutations({
decrement: 'decrement',
increment: 'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
add: 'add',
changeName: 'fn' // 将 `this.changeName(json)` 映射为 `this.$store.commit('fn', json)`
})
}
}
</script>
<style>
</style>
最后说一下mutation的使用场景,mutation在使用的是请不要涉及任何异步操作,如果你想改变count的值,你通过mutation中的两个异步事件,都改变了这个状态值,你怎么知道什么时候回调和哪个先回调呢,因此mutation用于管理同步事件,如果有异步操作,请用action.
猜您可能还喜欢
评论列表
发表评论
文章分类
文章归档
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
阅读排行
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5880)
- 2.Windows 10休眠文件更改存储位置(3857)
- 3.各大搜索网站网站收录提交入口地址(3483)
- 4.ECharts仪表盘实例及参数使用详解(3438)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(3434)
- 6.华为鸿蒙系统清除微信浏览器缓存方法(3183)
- 7.HTML5 WebSocket与C#建立Socket连接实现代码(3182)
- 8.CERT_HAS_EXPIRED错误如何解决(2975)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2610)
- 10.HBuilder编辑器格式化代码(2396)
