Skip to content
Advertisement

Passing multiple parameters to Vuex action

I have the following method in my Vue Component

loadMaintenances (query = {}) {
  this.getContractorMaintenances(this.urlWithPage, query).then((response) => {
    this.lastPage = response.data.meta.last_page
  })
}

I want to pass the parameters (this.urlWithPage, query) to my Vuex action as follows:

actions:{
  async getContractorMaintenances ({ commit }, url, query) {
    console.log(url);
    console.log(query);
    let response = await axios.get(url)

    commit('PUSH_CONTRACTOR_MAINTENANCES', response.data.data)

    return response
  },
}

The problem is that the first parameter url is returning a value but the second one query is returning undefined.

My mutation is as follows:

mutations: {
  PUSH_CONTRACTOR_MAINTENANCES (state, data) {
    state.contractor_maintenances.push(...data)
  },
}

How can I get a value from the second parameter?

Advertisement

Answer

The accepted answer to this also applies to actions, it expects two arguments: context and payload.

In order to pass multiple values you’ll have to send the data across as an object and destructure them:

async getContractorMaintenances ({ commit }, { url, query }) {
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement