[Vuex系列] - Mutation的具体用法
更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。
接下来我们还是用上一篇文章在state中存放的count为例,来看利用Mutation修改state的count属性。
利用commit来触发mutation函数
在mutation函数中添加count的add函数
const mutations = {
addNum (state) {
state.num++
},
add (state) {
state.count += 2
}
}
export default mutations
在组件中使用mutation进行实现叠加器
<template>
<div class="mutation">
<p>{{ count }}</p>
<button @click="addCount">+ADD</button>
</div>
</template> <script>
import store from '@/store/store'
export default {
computed: {
count () {
return this.$store.state.count
}
},
methods: {
addCount () {
store.commit('add')
}
}
}
</script>
Mutation的载荷(payload)
你可以向store.commit传入额外的参数,即mutation的载荷(payload):我们还是以上面累加器的例子来实现mutation函数的传参,来动态定义累加的数量。
在mutation.js中修改add方法
const mutations = {
addNum (state) {
state.num++
},
add (state, n) {
state.count += n
}
}
export default mutations
在组件中store.commit如何传参
<template>
<div class="mutation">
<p>{{ count }}</p>
<button @click="addCount">+ADD</button>
</div>
</template> <script>
import store from '@/store/store'
export default {
computed: {
count () {
return this.$store.state.count
}
},
methods: {
addCount () {
store.commit('add', 5)
}
}
}
</script>
在mutation传参(载荷)可以传递一个参数也可以传递一个对象。让我们修改下上面的例子
mutation.js文件中修改如下
const mutations = {
addNum (state) {
state.num++
},
add (state, payload) {
state.count += payload.amount
}
}
export default mutations
组件中修改如下
<template>
<div class="mutation">
<p>{{ count }}</p>
<button @click="addCount">+ADD</button>
</div>
</template> <script>
import store from '@/store/store'
export default {
computed: {
count () {
return this.$store.state.count
}
},
methods: {
addCount () {
store.commit('add', { amount: 10 })
}
}
}
</script>
在store.commit中可以进行对象风格的提交
依据上面的例子,我们将组件中内容修改如下
<template>
<div class="mutation">
<p>{{ count }}</p>
<button @click="addCount">+ADD</button>
</div>
</template> <script>
import store from '@/store/store'
export default {
computed: {
count () {
return this.$store.state.count
}
},
methods: {
addCount () {
store.commit({
type: 'add',
amount: 8
})
}
}
}
</script>
使用常量替代 Mutation 事件类型
使用常量替代mutation事件类型在各种Flux实现中是很常见的模式。这样可以使 linter之类的工具发挥作用,同时把这些常量放在单独的文件中可以让你的代码合作者对整个项目包含的mutation一目了然。这在在需要多人协作的大型项目中,这会很有帮助。
我们在store中新建mutation-types.js文件,文件内容如下
export const SOME_MUTATION = 'SOME_MUTATION'
在mutation.js文件内容如下
import { ADD } from './mutation-types'
const mutations = {
addNum (state) {
state.num++
},
[ADD] (state) {
state.count++
}
}
export default mutations
在组件中内容和之前一致
<template>
<div class="mutation">
<p>{{ count }}</p>
<button @click="addCount">+ADD</button>
</div>
</template> <script>
import store from '@/store/store'
export default {
computed: {
count () {
return this.$store.state.count
}
},
methods: {
addCount () {
store.commit('add')
}
}
}
</script>
在组件中使用this.$store全局属性来触发mutation函数
<template>
<div class="mutation">
<p>{{ count }}</p>
<button @click="add">+ADD</button>
</div>
</template> <script>
export default {
computed: {
count () {
return this.$store.state.count
}
},
methods: {
add () {
this.$store.commit('add')
}
}
}
</script>
在组件中使用mapMutations辅助函数
<template>
<div class="mutation">
<p>{{ count }}</p>
<button @click="add">+ADD</button>
</div>
</template> <script>
import { mapMutations } from 'vuex'
export default {
computed: {
count () {
return this.$store.state.count
}
},
methods: {
...mapMutations(['add'])
}
}
</script>
Mutation一条重要的原则就是要记住 mutation 必须是同步函数
[Vuex系列] - Mutation的具体用法的更多相关文章
- Spring3系列5-Bean的基本用法
Spring3系列5-Bean的基本用法 本篇讲述了Bean的基本配置方法,以及Spring中怎样运用Bean. 主要内容如下: 一. Spring中Bean的相互引用 二. Sp ...
- 学习javascript基础知识系列第二节 - this用法
通过一段代码学习javascript基础知识系列 第二节 - this用法 this是面向对象语言中的一个重要概念,在JAVA,C#等大型语言中,this固定指向运行时的当前对象.但是在javascr ...
- SpringBoot系列之@Conditional注解用法简介
SpringBoot系列之@Conditional注解用法简介 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档 @ ...
- SpringBoot系列之外部配置用法简介
SpringBoot系列之外部配置用法简介 引用Springboot官方文档的说法,官方文档总共列举了如下用法: 1.Devtools global settings properties on yo ...
- [Vuex系列] - Module的用法(终篇)
于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿.为了解决以上问题,Vuex 允许我们将 store 分割成模块(module).每 ...
- [Vuex系列] - 细说state的几种用法
state 存放的是一个对象,存放了全部的应用层级的状态,主要是存放我们日常使用的组件之间传递的变量. 我们今天重点讲解下state的几种用法,至于如何从头开始创建Vuex项目,请看我写的第一个文章. ...
- 挑战全网最幽默的Vuex系列教程:第三讲 Vuex旗下的Mutation
写在前面 上一讲「Vuex 旗下的 State 和 Getter」,告诉了我们怎么去使用仓库 store 中的状态数据.当然,光会用肯定还不够,大部分的应用场景还得对这些状态进行操控,那么具体如何操控 ...
- [Vuex系列] - Vuex中的getter的用法
Vuex 允许我们在store中定义“getter”(可以认为是store的计算属性).就像计算属性一样,getter的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算. g ...
- vuex之Mutation(三)
说明 既然我们可以取出数据,就可以修改数据,而修改数据并不是像修改data的数据一样,直接 this.xxx = xxx,这样有一个问题,在实际开发中,state的数据一般会多个组件共享,如果可以使用 ...
随机推荐
- 在Springmvc普通类@Autowired注入request为null解决方法
在Springmvc普通类@Autowired注入request为null解决方法 在类中加入以下注入request对象的代码,运行时发现request为null,注入失败.在@Controlle ...
- windows的mysql无法启动 服务没有报告任何错误
相信很多人都遇到过安装Mysql的时候出现各种各样的问题,今天小编就教大家解决window下mysql服务没有报告任何错误的情况下无法启动 的问题.本文所用的mysql版本是5.7以上版本,解决方法: ...
- MySQL导入utf8编码的CSV文件
首先,作为测试,我们在这里创建一个名为testdb的数据库,和一个名为test_table的表: create database if not exists testdb default charse ...
- [c++]struct timeval
struct timeval { time_t tv_sec; // seconds long tv_usec; // microseconds }; re 1. struct timespec 和 ...
- ELK之在CentOS7.5上使用rpm包安装配置ELK7版本
一,安装环境查看 二,软件版本选用 jdk 1.8.0_171 elasticsearch 7.1.1 kibana 7.1.1 logstash 7.1.1 三,安装配置 1,安装JDK 过程不详述 ...
- iOS-self.用法
关于self.用法的一些总结 2010-01-10 21:46 最近有人问我关于什么时候用self.赋值的问题, 我总结了一下, 发出来给大家参考. 有什么问题请大家斧正. 关于什么时间用self. ...
- 八个免费的Vue图标库
1. Vue-awesome 也许大家知道 Font-awesome 之类比较流行的图标库,就像各大组件库都有各自版本一样,它也有Vue的版本 Github地址:https://github.com/ ...
- python 修改文件内容3种方法
原文链接:https://www.cnblogs.com/wc-chan/p/8085452.html def alter(file,old_str,new_str): ""&qu ...
- C语言--函数嵌套调用
一.实验作业(6分) 本周作业要求: 选一题PTA题目介绍. 学习工程文件应用,设计实现学生成绩管理系统. 学生成绩管理系统要求 设计一个菜单驱动的学生成绩管理程序,管理n个学生m门考试科目成绩,实现 ...
- [转帖]APP逆向神器之Frida【Android初级篇】
APP逆向神器之Frida[Android初级篇] https://juejin.im/post/5d25a543e51d455d6d5358ab 说到逆向APP,很多人首先想到的都是反编译,但是单看 ...