简体   繁体   English

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

[英]vuex- dynamic modules don't have unique data

I have an async api call where I get an array of objects and then I map that to dynamically registered modules in my store. 我有一个异步api调用,在该调用中获取对象数组,然后将其映射到商店中动态注册的模块。 Something like this: 像这样:

dispatch 调度

// 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);

The list/${doctor.id} is the dynamic module list/${doctor.id}是动态模块

action in availabilities module 可用性模块中的操作

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

mutation 突变

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

When I check my logs for the console.log above each doctor has different arrays of time slots Exactly the data I want. 当我在上方检查console.log日志时,每个医生都有不同的时隙阵列。正是我想要的数据。 However, in the vue developer tools and what is being rendered is just the last doctor's timeslots for all of the doctors. 但是,在vue开发人员工具中,呈现的只是所有医生的最后一个医生的时间段。 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'm seeing the last doctor's availabilities for every doctor? 有人对我为什么要看到每位医生的最后一位医生有任何想法吗?

It looks like you are assigning the same array (timeslots) to all doctors. 看来您正在向所有医生分配相同的阵列(时隙)。

When you add an element to the array for one doctor, you mutate the array that all doctors are sharing. 当您为一个医生的阵列添加元素时,您将对所有医生共享的阵列进行突变。

However with the little code you show, it's difficult to know where is the exact problem. 但是,由于只显示了很少的代码,所以很难知道确切的问题在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM