javascript 如何将几个动作的参数与上下文一起发送到url Vuex?

crcmnpdw  于 2023-05-16  发布在  Java
关注(0)|答案(1)|浏览(90)

https://stackblitz.com/edit/vue-hrqfyc?file=src%2Fstore%2Findex.js,src%2FApp.vue,src%2Fcomponents%2FArrivalDate.vue,src%2Fcomponents%2FArrivalPoint.vue,src%2Fcomponents%2FDepartureDate.vue,src%2Fcomponents%2FDeparturePoint.vue,src%2Fcomponents%2FFlight.vue,src%2Fcomponents%2FFlightList.vue,src%2Fmain.js
我正在尝试如何将几个动作的参数连同上下文一起发送到URL,因为现在我得到了400错误。selectedPoint和departurePoint来自子组件,并在其数据中设置为变量。所以它看起来像这样:

async getFlights(context, selectedPoint, departurePoint) {
        
      const res = await fetch(
        `http://api.travelpayouts.com/v2/prices/month-matrix?currency=rub&origin=${selectedPoint}&destination=${departurePoint}&show_to_affiliates&token=${context.state.token}`,
      );
      if (res.ok) {
        let result = await res.json();
        context.commit('setFlights', result.data);
      }
      return res.ok;
    },
n6lpvg4x

n6lpvg4x1#

一个action只接受两个参数,context和payload。
所以这里你需要在传递多个参数时使用object。

async getFlights(context, { selectedPoint, departurePoint }) {
    
  const res = await fetch(
    `http://api.travelpayouts.com/v2/prices/month-matrix?currency=rub&origin=${selectedPoint}&destination=${departurePoint}&show_to_affiliates&token=${context.state.token}`,
  );
  if (res.ok) {
    let result = await res.json();
    context.commit('setFlights', result.data);
  }
  return res.ok;
},

当调用动作时,将值作为对象传递。
https://vuex.vuejs.org/api/#actions

相关问题