vuex05Action与mapAction

简介: vuex05Action与mapAction

Action是什么

Action 提交的是 mutation,而不是直接变更状态。

当然Action也获取state,getter

Action 可以包含任意异步操作。

Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit  提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。

const store = new Vuex.Store({
    state: {
        name: "old name",
        age: 18,
        sex: "女",
    },
    mutations: {
        changName(state) { 
          state.name = "newName"   
        },
        addAge(state, num) {
            state.age += num
        },
        changSex(state) {
            state.sex = state.age + "男"
        }
    },
    actions: {
        useActionToChangName(context) {
        // 这里的context也可以写成{commit,state}
            context.commit('changName')
            context.commit('addAge',10)
        }
    }
})

使用Dispatch来触发事件

methods: {
    changeNameAndAge() {
      this.$store.dispatch({ type: "useActionToChangName" });
    },
    或者
     changeNameAndAge() {
      this.$store.dispatch ("useActionToChangName" );
    },
}

mapActions

  1. 引入
import { mapActions } from 'vuex'
  1. 使用
 methods: {
    ...mapActions([
      'useActionToChangName', // 将 `this.useActionToChangName()` 映射为 `this.$store.dispatch('useActionToChangName')`
      // `mapActions` 也支持载荷:
      'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
    ]),
    ...mapActions({
      add: 'useActionToChangName' // 将 `this.add()` 映射为 `this.$store.dispatch('useActionToChangName')`
    })
  }


相关文章
|
16天前
|
JavaScript API 开发工具
uni.app cell的用法以及相关api
uni.app cell的用法以及相关api
31 0
|
7月前
|
JavaScript 前端开发
为什么import store from ‘./store‘和 ‘./store/index‘一样
为什么import store from ‘./store‘和 ‘./store/index‘一样
|
16天前
|
前端开发 搜索推荐 JavaScript
Spartacus Cart item 点击了 remove 之后 HTTP Delete 请求的触发逻辑 - Adapter
Spartacus Cart item 点击了 remove 之后 HTTP Delete 请求的触发逻辑 - Adapter
15 0
|
16天前
|
存储 JavaScript
深入理解 Vuex 中的this.$store.dispatch方法
深入理解 Vuex 中的this.$store.dispatch方法
深入理解 Vuex 中的this.$store.dispatch方法
|
前端开发 JavaScript API
vuex为什么不建议在action中修改state
在最近的一次需求开发过程中,有再次使用到Vuex,在状态更新这一方面,我始终遵循着官方的“叮嘱”,谨记“一定不要在action中修改state,而是要在mutation中修改”;于是我不禁产生了一个疑问:Vuex为什么要给出这个限制,它是基于什么原因呢?带着这个疑问我查看Vuex的源码,下面请大家跟着我的脚步,来一起揭开这个问题的面纱。
|
开发工具 Android开发
解决Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
解决Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
174 0
解决Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
SAP Spartacus user-addresses.effect.ts里发送地址加载成功的action,会触发我们自己的reducer
SAP Spartacus user-addresses.effect.ts里发送地址加载成功的action,会触发我们自己的reducer
78 0
SAP Spartacus user-addresses.effect.ts里发送地址加载成功的action,会触发我们自己的reducer
SAP Spartacus 成功读取 Cart 之后,如何将 payload 插入全局 state
SAP Spartacus 成功读取 Cart 之后,如何将 payload 插入全局 state
SAP Spartacus 成功读取 Cart 之后,如何将 payload 插入全局 state
SAP Spartacus读取User Address的action是如何被Effect接收的
SAP Spartacus读取User Address的action是如何被Effect接收的
65 0
SAP Spartacus读取User Address的action是如何被Effect接收的
http://www.vxiaotou.com