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

如果需要用到actions的情况,如下图,应该是组件调用Actions,Actions提交Mutations,Mutations修改state
image.png

定义

vuex 3 和 vue2

src/store/index.js

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. export default new Vuex.Store({
  5. state: {
  6. share:100
  7. },
  8. getters:{
  9. shareFloat:function (state) {
  10. return "¥" + state.share
  11. }
  12. },
  13. mutations: {
  14. addShare:function(state){
  15. state.share++;
  16. }
  17. },
  18. actions: {
  19. //不是直接改变变量,而是提交mutations的函数
  20. asyncAddShare:function(context,obj){
  21. setTimeout(()=>{
  22. //也可以通过 context.state 和 context.getters 来获取 state 和 getters
  23. context.commit("addShare",obj)
  24. },3000)
  25. }
  26. },
  27. modules: {
  28. }
  29. })

vuex 4 和 vue3

src/store/index.js

  1. import { createStore } from "vuex"
  2. const store = createStore({
  3. state() {
  4. return {
  5. counter: 100,
  6. };
  7. },
  8. mutations: {
  9. increment(state) {
  10. state.counter++;
  11. },
  12. decrement(state) {
  13. state.counter--;
  14. },
  15. },
  16. actions: {
  17. // 放函数
  18. // 1.简单使用,参数context可以调用mutations方法
  19. incrementAction(context, n) {
  20. console.log(n)
  21. setTimeout(() => {
  22. context.commit('increment')
  23. }, 1000);
  24. },
  25. // 2.context的其他属性
  26. // context = {commit, dispatch, state, rootState, getters, rootGetters}
  27. decrementAction({ commit, dispatch, state, rootState, getters, rootGetters }) {
  28. commit("decrement")
  29. },
  30. }
  31. });
  32. export default store;

参数 context

image.png

其他参数

image.png 打印看看
image.png
因此写法可以见上vuex 4 和 vue3,用一个对象{commit, dispatch, state, rootState, getters, rootGetters}代替context

commit:提交mutations的方法
dispatch:调用其他的actions方法
getters:使用vuex的计算属性getters
rootGetters、rootState :分模块的时候,拿到根仓库(最顶级数据仓库)的计算属性getters和数据state
state:数据仓库

使用

vuex 3 和 vue2 ,或vue3 选项api

在组件内使用$store.dispatch(“asyncAddShare”) 的方法使用actions的函数

  1. <template>
  2. <div>
  3. test2
  4. <el-button @click="add">add</el-button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. };
  12. },
  13. methods: {
  14. add:function () {
  15. // 1、简单用法
  16. // dispatch可以有第二个参数,就是定义那里的n,传入数据或对象等
  17. this.$store.dispatch("incrementAction")
  18. // 2、对象用法
  19. this.$store.dispatch({
  20. type:"incrementAction",
  21. n:100
  22. })
  23. }
  24. },
  25. };
  26. </script>
  27. <style scoped>
  28. </style>

vue3 组合api

  1. <template>
  2. <div>
  3. test2
  4. <el-button @click="add">add</el-button>
  5. </div>
  6. </template>
  7. <script>
  8. import {useStore} from 'vuex'
  9. export default {
  10. setup(){
  11. const store = useStore()
  12. const add = ()=>{
  13. // 1、简单用法,参数用法和上面选项api一样
  14. store.dispatch("incrementAction")
  15. // 2、对象用法
  16. store.dispatch({
  17. type:"incrementAction",
  18. n:100
  19. })
  20. }
  21. return {
  22. add
  23. }
  24. }
  25. };
  26. </script>
  27. <style scoped>
  28. </style>

网络请求

可以通过actions获取网络请求的数据,数据直接提交mutations修改对应的state
image.png

异步操作(返回Promise)

可以在定义actions函数时,返回Promise对象,后续其他地方使用时,可以通过then来接收执行状态
image.png

其他组件内使用,可以等actions执行完后再执行自定义的内容
image.png

Devtools

正确使用就会正常记录到devtools里面
image.png

批量使用 mapActions

选项API

数组写法和对象写法
image.png

组合API

image.png