Skip to content

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

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

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

Activity

wenyejie

wenyejie commented on Jun 21, 2019

@wenyejie

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

一个对外, 一个对内

NingLin00

NingLin00 commented on Jul 10, 2019

@NingLin00

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

xujunfa

xujunfa commented on Jul 24, 2019

@xujunfa

Action

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

Mutation

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

WenJieLi1998 commented on Apr 17, 2020

@WenJieLi1998

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

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

yxllovewq

yxllovewq commented on Mar 10, 2022

@yxllovewq

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

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

        @wenyejie@haizhilin2013@xujunfa@WenJieLi1998@NingLin00

        Issue actions

          [vue] vuex的action和mutation的特性是什么?有什么区别? · Issue #390 · haizlin/fe-interview