Skip to content

[vue] vuex中actions和mutations有什么区别? #537

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

[vue] vuex中actions和mutations有什么区别?

Activity

zyronon

zyronon commented on Jul 2, 2019

@zyronon

actions可以异步,mutations不可以

joyeeLee

joyeeLee commented on Jul 25, 2019

@joyeeLee

actions是异步处理state,mutations是同步处理state,这样说对吗

zejunking

zejunking commented on Aug 20, 2019

@zejunking

mutations可以直接修改state,但只能包含同步操作,同时,只能通过提交commit调用(尽量通过Action或mapMutation调用而非直接在组件中通过this.$store.commit()提交)
actions是用来触发mutations的,它无法直接改变state,它可以包含异步操作,它只能通过store.dispatch触发

liuxiaoyang1

liuxiaoyang1 commented on Nov 19, 2019

@liuxiaoyang1

mutations 是同步的,直接操作,提交commit就可以了,而actions 是异步的操作,比如axios 请求后台数据

cherry728

cherry728 commented on Jun 15, 2020

@cherry728

action是异步的更改state,本质是dispatch一个action,在action里面其实也是commit一个mutation的。mutation里面是直接对state进行更改

crush2020

crush2020 commented on Jan 29, 2021

@crush2020

actions:主要用来处理异步,提交的是mutations,在组件中用dispatch()派发
mutations:用来直接修改state的在组件中用this.$store.commit()触发
其两个都可以用辅助函数mapActions
详细参考地址:https://vuex.vuejs.org/zh/guide/actions.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@cherry728@zyronon@zejunking@liuxiaoyang1

        Issue actions

          [vue] vuex中actions和mutations有什么区别? · Issue #537 · haizlin/fe-interview