Passing multiple parameters to Vuex action
Asked Answered
S

1

9

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?

Selah answered 18/1, 2021 at 22:2 Comment(0)
D
10

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 }) {
Devora answered 18/1, 2021 at 22:11 Comment(4)
When I do async getContractorMaintenances ({commit} , { url, query }) { console.log(url); console.log(query); both url and query are returned as undefined.Selah
Are you passing it an object with url and query properties?Devora
How do I pass them as an object? I'm not understanding that part. My url http://127.0.0.1?page=1 and query {status=new}Selah
It would have to be in the form of const payload = { url: 'http://127.0.0.1?page=1', query: 'status=new' }Devora

© 2022 - 2024 — McMap. All rights reserved.