Skip to content

[vue] vuex的action和mutation的特性是什么?有什么区别? #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
haizhilin2013 opened this issue Jun 20, 2019 · 5 comments
Open
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

[vue] vuex的action和mutation的特性是什么?有什么区别?

@haizhilin2013 haizhilin2013 added the vue vue label Jun 20, 2019
@wenyejie
Copy link

action: 负责与外界交互, 比如请求数据(好像一般都是)
mutation: 负责: 内部state变更

一个对外, 一个对内

@NingLin00
Copy link

action: 通过执行 commit()来触发mutation的调用, 间接更新state ,
组件中通过$store.dispatch('action名称') 触发action,
可以包含异步代码(定时器, ajax)
mutation是一个对象 包含多个直接更新state的方法(回调函数) ,只能包含同步的代码, 不能写异步代码

@xujunfa
Copy link

xujunfa commented Jul 24, 2019

Action

  • 一些对 State 的异步操作可放在 Action 中,并通过在 Action 中 commit Mutation 变更状态
  • Action 可通过 store.dispatch() 方法触发,或者通过 mapActions 辅助函数将 vue 组件的 methods 映射成 store.dispatch() 调用

Mutation

  • 在 vuex 的严格模式下,Mutaion 是 vuex 中改变 State 的唯一途径
  • Mutation 中只能是同步操作
  • 通过 store.commit() 调用 Mutation

@WenJieLi1998
Copy link

Action
1.可以包含任意异步操作
2.它提交的是mutation,而不是直接变更状态

Mutation
1.唯一一个可以修改state的途径
2.必须是同步函数

@yxllovewq
Copy link

mutations用于变更state,只能有同步代码。
actions用于提交mutaion变更state,可以有异步代码。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vue vue
Projects
None yet
Development

No branches or pull requests

6 participants