将多个参数传递给 Vuex 动作

时间:2021-01-18 22:02:31

标签: javascript vue.js vuejs2 vuex

我的 Vue 组件中有以下方法

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

我想将参数 (this.urlWithPage, query) 传递给我的 Vuex 操作,如下所示:

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
  },
}

问题是第一个参数 url 返回一个值,而第二个参数 query 返回 undefined

我的突变如下:

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

如何从第二个参数中获取值?

1 个答案:

答案 0 :(得分:3)

this 的公认答案也适用于 actions它需要两个参数contextpayload

为了传递多个值,您必须将数据作为对象发送并对其进行解构:

async getContractorMaintenances ({ commit }, { url, query }) {