首页 文章

vuex-动态模块没有唯一数据

提问于
浏览
0

我有一个异步api调用,我得到一个对象数组,然后我将它映射到我的商店中动态注册的模块 . 像这样的东西:

派遣

// before this dispatch some api call happens and inside the promise
// iterate over the array of data and dispatch this action
dispatch(`list/${doctor.id}/availabilities/load`, doctor.availabilities);

list/${doctor.id} 是动态模块

可用性模块中的操作

load({ commit }, availabilities) {
  const payload = {
    id: availabilities.id,
    firstAvailable: availabilities.firstAvailable,
    timeslots: [],
  };
  // then a bunch of code that maps the availabilities to a specific format changing the value of payload.timeslots 
  commit('SET_AVAILABILITIES', payload)
}

突变

[types.SET_TIMESLOTS](state, payload) {
  console.log(payload);
  state.firstAvailable = payload.firstAvailable;
  state.id = payload.id;
  state.timeslots = payload.timeslots;
}

当我检查我的日志上面的 console.log 时,每个医生都有不同的时隙阵列正好是我想要的数据 . 然而,在vue开发人员的工具和正在呈现的内容只是最后一位医生's timeslots for all of the doctors. All of my business logic is happening in the load action and the payload in the mutation is the correct data post business logic. Anyone have any ideas why I'看到最后一位医生对每位医生的可用性?

1 回答

  • 0

    看起来您正在为所有医生分配相同的阵列(时间段) .

    当您为一位医生向阵列添加元素时,您会改变所有医生共享的阵列 .

    但是,如果您显示的代码很少,则很难知道确切的问题在哪里 .

相关问题