[Vuejs]-Vuex: Sending arguments to actions from within vuex

0👍

Provided your action returns a value or a promise that resolves with the value you want, eg

actionOne: async ({ commit }, payload) => {
  // do something async, commit values, etc
  return actionResponse
}

then you can keep chaining promises like so

axios.get(data.url)
  .then(({ data }) => context.dispatch("actionOne", data))
  .then(actionResponse => context.dispatch("actionTwo", actionResponse))

Note the implicit return value from the arrow functions. The .then() method always returns a new promise that resolves with the return value.

In longer form, this looks like

axios.get(data.url)
  .then(({ data }) => {
    return context.dispatch("actionOne", data))
  })
  .then(actionResponse => {
    return context.dispatch("actionTwo", actionResponse))
  })

Leave a comment