首页 > 其他分享 >vuex中actions和mutations区别

vuex中actions和mutations区别

时间:2023-08-16 11:33:27浏览次数:40  
标签:异步 状态 vuex mutations actions state data

定义: 在Vuex中,actions和mutations是两个核心概念,用于管理应用程序状态的变化。

一: Mutations

mutations是用于修改Vuex状态的唯一方法。它们是同步操作,意味着它们必须是纯函数,以确保状态的可预测性。这意味着mutations应该只用于同步操作,例如在响应用户事件时更新状态。它们不应该包含任何异步代码,如API调用。

举个例子,下面的代码演示了如何定义一个名为increment的mutation,用于将state中的count属性增加


const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
}
}
})

store.commit('increment') // 调用mutation
1
2
3
4
5
6
7
8
9
10
11
12
13
二:Actions

actions用于执行异步操作或包含异步操作的操作序列。它们可以包含任何异步代码,例如API调用或其他异步操作,但不能直接修改状态。相反,它们通过commit触发mutations来间接修改状态。

举个例子,下面的代码演示了如何定义一个名为fetchData的action,它会异步获取数据并提交mutation来更新状态:


const store = new Vuex.Store({
state: {
data: null
},
mutations: {
setData(state, data) {
state.data = data
}
},
actions: {
async fetchData({ commit }) {
const response = await fetch('https://api.example.com/data')
const data = await response.json()
commit('setData', data)
}
}
})

store.dispatch('fetchData') // 调用action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
总之,mutations和actions有不同的角色和职责。mutations用于同步状态的修改,actions用于执行异步操作并间接修改状态。
————————————————
版权声明:本文为CSDN博主「JoinMao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_56960330/article/details/129420007

标签:异步,状态,vuex,mutations,actions,state,data
From: https://www.cnblogs.com/chinasoft/p/17633600.html

相关文章

  • Programming abstractions in C阅读笔记p111-p113: boilerplate
    《ProgrammingAbstractionsInC》学习第47天,p111-p113,总结如下:一、技术总结1.boilerplate/**File:random.h*Version:1.0*LastmodifiedonFriJul2216:44:361994byeroberts*-----------------------------------------------------*Thisinterfacepr......
  • Programming abstractions in C阅读笔记p111-p113: boilerplate
    《ProgrammingAbstractionsInC》学习第47天,p111-p113,总结如下:一、技术总结1.boilerplate/**File:random.h*Version:1.0*LastmodifiedonFriJul2216:44:361994byeroberts*-----------------------------------------------------*Thisinterfaceprovi......
  • vuex辅助函数使用
    一:mapState的使用此函数返回一个对象,生成计算属性-当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。mapState可以声明多个需要在组件中引入:需要在组件中引入:import{mapState}from'vuex'...mapState({/......
  • Programming abstractions in C阅读笔记:p107-p110
    《ProgrammingAbstractionsInC》学习第46天,p107-p110,3.1小节——“Theconceptofinterface”,总结如下:一、技术总结1.clientp108,调用library的program称为client。2.interfacep108,"Todoso,thechapterfocusesontheboundarybetweenalibraryanditsclients,wh......
  • Programming abstractions in C阅读笔记:p107-p110
    《ProgrammingAbstractionsInC》学习第46天,p107-p110,3.1小节——“Theconceptofinterface”,总结如下:一、技术总结1.  clientp108,调用library的program称为client。2.  interfacep108,"To do so, the chapter focuses on the boundary between a library and ......
  • mysql在开启group_replication后,报错ERROR 3092,This member has more executed transa
    问题描述:mysql在开启group_replication后,报错ERROR3092,Thismemberhasmoreexecutedtransactionsthanthosepresentinthegroup,如下所示:数据库:MySQL8.0.27系统:rhel7.31、异常重现Slave01[(none)]>startgroup_replication;ERROR3092(HY000):Theserverisnotc......
  • IfcModulusOfSubgradeReactionSelect
    IfcModulusOfSubgradeReactionSelect类型定义垫层测量,表示每个区域的结构面项目的垫层。TRUE表示无限刚度(刚度)。FALSE表示无硬度(释放)。数值表示有限的线性弹性刚度。 IFC4中的新型。 EnumerationdefinitionConstantDescriptionIfcBoolean IfcModulusOfSubgrade......
  • Vue进阶(幺肆捌):Vuex 辅助函数详解
    (文章目录)一、前言一般情况下,如果需要访问vuex.store中state存放的数据,需要使用this.$store.state.属性名方式。显然,采取这样的数据访问方式,代码略显繁杂,辅助函数为了解决繁杂行问题应运而生。二、辅助函数通过辅助函数mapGetters、mapState、mapActions、mapMutations,把vuex.......
  • Programming abstractions in C阅读笔记:p91-p106
    《ProgrammingAbstractionsInC》学习第45天,p91-p102,完成第二章内容学习。总结如下:一、技术总结1.垃圾回收p91,"Somelanguage,includingJavasupportasystemfordynamicallocationthatactivelygoesthroughtoseewhatpartsofitareused,freeinganystorageth......
  • Programming abstractions in C阅读笔记:p88-p90
    《ProgrammingAbstractionsInC》学习第44天,p88-p90总结。一、技术总结1.内存分配内存分配可以分为:staticallocation、automaticallocation、dynamicallocation。内存分配使用的函数为:malloc()。二、英语总结1."up to this point"是什么意思?答:point: a particular......