[vue] vuex中actions和mutations有什么区别?
Activity
zyronon commentedon Jul 2, 2019
actions可以异步,mutations不可以
joyeeLee commentedon Jul 25, 2019
actions是异步处理state,mutations是同步处理state,这样说对吗
zejunking commentedon Aug 20, 2019
mutations可以直接修改state,但只能包含同步操作,同时,只能通过提交commit调用(尽量通过Action或mapMutation调用而非直接在组件中通过this.$store.commit()提交)
actions是用来触发mutations的,它无法直接改变state,它可以包含异步操作,它只能通过store.dispatch触发
liuxiaoyang1 commentedon Nov 19, 2019
mutations 是同步的,直接操作,提交commit就可以了,而actions 是异步的操作,比如axios 请求后台数据
cherry728 commentedon Jun 15, 2020
action是异步的更改state,本质是dispatch一个action,在action里面其实也是commit一个mutation的。mutation里面是直接对state进行更改
crush2020 commentedon Jan 29, 2021
actions:主要用来处理异步,提交的是mutations,在组件中用dispatch()派发
mutations:用来直接修改state的在组件中用this.$store.commit()触发
其两个都可以用辅助函数mapActions
详细参考地址:https://vuex.vuejs.org/zh/guide/actions.html