174

I have the following setup for my actions:

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

I want to be able to call one action from within another, so in this example I want to be able to call get2() from within get1(). Is this possible, and if so, how can I do it?

NearHuscarl
  • 12,341
  • 5
  • 39
  • 69
muttley91
  • 11,995
  • 27
  • 97
  • 144

5 Answers5

347

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.

kissu
  • 6,476
  • 5
  • 15
  • 33
thanksd
  • 44,567
  • 20
  • 126
  • 130
5

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);
    }
}
kissu
  • 6,476
  • 5
  • 15
  • 33
Rishabh Agrawal
  • 1,452
  • 21
  • 32
4

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 })
  }
}
17axaH
  • 300
  • 1
  • 6
3

we can pass parameters also while dispatching.

dispatch('fetchContacts', user.uid);
kissu
  • 6,476
  • 5
  • 15
  • 33
sau0409
  • 103
  • 5
0
export actions = {
  GET_DATA (context) {
     // do stuff
     context.dispatch('GET_MORE_DATA');
  },

  GET_MORE_DATA (context) {
    // do more stuff
  }
}
kissu
  • 6,476
  • 5
  • 15
  • 33
Dazzle
  • 2,196
  • 3
  • 18
  • 40