美文网首页
vue状态管理模式vuex-Action

vue状态管理模式vuex-Action

作者: 清风昙 | 来源:发表于2022-08-23 00:29 被阅读0次

Action 类似于 mutation,不同在于:
1.Action 提交的是 mutation,而不是直接变更状态
2.Action 可以包含任意异步操作
action实例

const store = createStore({
  state: {
    count: 0
   },
    mutations: {
      increment (state) {
        state.count++
      }
    },
    actions: {
      increment (context) {
        context.commit('increment')
      }
     }
})

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

actions: {
  increment ({ commit }) {
    commit('increment')
  }
}
分发Action

Action通过store.dispatch方法触发

store.dispatch('increment')

在 action 内部可以执行异步操作

actions: {
  increment ({ commit }) {
    setTimeout(() => {
      commit('increment')
    }, 1000)
  }
}

Actions 支持同样的载荷方式和对象方式进行分发

// 载荷形式分发
store.dispatch('increment', {
  amount: 1
})
// 对象形式分发
store.dispatch({
  type: 'increment',
  amount: 1
})
组件中分发Action

在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store)

import { mapActions } from 'vuex'

export default {
  methods: {
    ...mapActions([
      'increment', // 将'this.increment()'映射为'this.$store.dispatch('increment')'
      // mapActions 也支持载荷
      'incrementBy' // 将'this.incrementBy(amount)'映射为'this.$store.dispatch('incrementBy', amount)'
    ]),
    ...mapActions({
      add: 'increment' // 将'this.add()'映射为'this.$store.dispatch('increment')'
    })
  }
}
组合Action

Action 通常是异步的,那么如何知道 action 什么时候结束呢?
首先,你需要明白 store.dispatch 可以处理被触发的 action 的处理函数返回的 Promise,并且 store.dispatch 仍旧返回 Promise

actions: {
  actionA({ commit }) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        commit('someMutation')
        resolve()
       }, 1000)
    })
  }
}

在组件中可以这样使用

store.dispatch('actionA').then(() => {

})

在另外一个action中可以

actions: {
  actionB ({ dispatch, commit }) {
    return dispatch('actionA').then(() => {
      commit('someOtherMutation')
    })
  }
}

如果使用async/await,可以如下组合action

// 假设getData()和getOtherData()返回的是Promise
actions: {
  async actionA ({ commit }) {
    commit('getData', await getData())
  },
  async actionB ({ dispatch,commit }) {
    await dispatch('actionA') // 等待actionA完成
    commit('getOtherData', await getOtherData())
  }
}

相关文章

  • vue状态管理模式vuex-Action

    Action 类似于 mutation,不同在于:1.Action 提交的是 mutation,而不是直接变更状态...

  • 2018-04-01

    什么是vuex? Vuex 是一个专为Vue.js用程序开发的状态管理模式。 状态管理模式中的状态是指什么状态?为...

  • 关于vuex

    1、vuex是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。 什么是“状态管理模式”? ...

  • Vuex知识整理

    Vuex是什么? Vuex是为了Vue.js应用程序开发的状态管理模式。那什么是“状态管理模式”呢?通俗来讲,它就...

  • vue基础(二) - Vuex

    简介 Vuex 是 专为 Vue 开发的状态管理模式。通常用于跨页面共享数据。 状态管理模式:它采用集中式的存储,...

  • 15-Vuex基础

    Vue之vuex状态管理 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。 这个Vuex包含以下几...

  • Vuex - 1 - 敲门

    1.Vuex的意义 为Vue.js应用开发的状态管理模式以一个全局单例模式管理组件共享的状态 2.状态管理模式 2...

  • vuex

    vuex是什么 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式也是为 Vue.js 设计的状态管理...

  • Vue学习笔记

    0.参考文档 理解vuex -- vue的状态管理模式 vuex最简单、最详细的入门文档 vue+webpack项...

  • vuex基础

    vuex Vuex是什么呢?它是Vue的状态管理模式,在使用vue的时候,需要在vue中各个组件之间传递值是很痛苦...

网友评论

      本文标题:vue状态管理模式vuex-Action

      本文链接:https://www.haomeiwen.com/subject/zcdbgrtx.html