vue store action与Mutation与getters的具体用法
更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。
接下来我们还是用上一篇文章在state中存放的count为例,来看利用Mutation修改state的count属性。
利用commit来触发mutation函数
在mutation函数中添加count的add函数
1
2
3
4
5
6
7
8
9
|
const mutations = { addNum (state) { state.num++ }, add (state) { state.count += 2 } } export default mutations |
在组件中使用mutation进行实现叠加器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<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方法
1
2
3
4
5
6
7
8
9
10
|
const mutations = { addNum (state) { state.num++ }, add (state, n) { state.count += n } } export default mutations |
在组件中store.commit如何传参
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<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文件中修改如下
1
2
3
4
5
6
7
8
9
10
|
const mutations = { addNum (state) { state.num++ }, add (state, payload) { state.count += payload.amount } } export default mutations |
组件中修改如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<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中可以进行对象风格的提交
依据上面的例子,我们将组件中内容修改如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<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文件,文件内容如下
1
|
export const SOME_MUTATION = 'SOME_MUTATION' |
在mutation.js文件内容如下
1
2
3
4
5
6
7
8
9
10
11
|
import { ADD } from './mutation-types' const mutations = { addNum (state) { state.num++ }, [ADD] (state) { state.count++ } } export default mutations |
在组件中内容和之前一致
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<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函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<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辅助函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<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 必须是同步函数
我们可以认为,【getters】是store的计算属性
1、action主要处理的是异步的操作,mutation必须同步执行,而action就不受这样的限制,也就是说action中我们既可以处理同步,也可以处理异步的操作
2、action改变状态,最后是通过提交mutation
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2018-05-03 Kubernetes PV/PVC使用实践
2018-05-03 Kubernetes集群中Service的滚动更新