Call an action from within another action

后端 未结 5 729
一个人的身影
一个人的身影 2021-01-30 05:03

I have the following setup for my actions:

get1: ({commit}) => {
  //things
  this.get2(); //this is my question!
},
get         


        
相关标签:
5条回答
  • 2021-01-30 05:11

    You can access the dispatch method through the first argument (context):

    export const actions = {
      get({ commit, dispatch }) {
        dispatch('action2')
      }
    }

    However, if you use namespaced you need to specify an option:

    export const actions = {
      get({ commit, dispatch }) {
        dispatch('action2', {}, { root: true })
      }
    }

    0 讨论(0)
  • 2021-01-30 05:11
    export actions = {
      GET_DATA (context) {
         // do stuff
         context.dispatch('GET_MORE_DATA');
      },
    
      GET_MORE_DATA (context) {
        // do more stuff
      }
    }
    
    0 讨论(0)
  • we can pass parameters also while dispatching.

    dispatch('fetchContacts', user.uid);
    
    0 讨论(0)
  • 2021-01-30 05:28

    for actions that does not require payload

    actions: {
        BEFORE: async (context, payload) => {
        },
        AFTER: async (context, payload) => {
            await context.dispatch('BEFORE');
        }
    }
    

    for actions that does require payload

    actions: {
        BEFORE: async (context, payload) => {
        },
        AFTER: async (context, payload) => {
            var payload = {}//prepare payload
            await context.dispatch('BEFORE', payload);
        }
    }
    
    0 讨论(0)
  • 2021-01-30 05:34

    You have access to the dispatch method in the object passed in the first parameter:

    get1: ({ commit, dispatch }) => {
      dispatch('get2');
    },
    

    This is covered in the documentation.

    0 讨论(0)
提交回复
热议问题