【问题标题】:Must click two times for Vue/Vuex to update arrayVue/Vuex 必须点击两次才能更新数组
【发布时间】:2020-11-02 19:55:19
【问题描述】:

所以我是 Vue/Vuex 的新手,我完全不知道我在这里做错了什么。 我有一个点击时调用的方法,如下所示:

Export (item) {
  this.exportObj = {
    start: this.dates[0],
    end: this.dates[1],
    userid: item.id,
  };

  this.getAllByFilter(this.exportObj);

  if (this.dateRangeText == "No dates seleceted") {
    this.disValue = true;
  } else {
    this.disValue = false;
  }

  if (this.exportData.events.length > 0) {
    this.exportData.events = [];
    console.log("Emptyed array because there was already data there!");
  } else {
    console.log("Well this was empty already :O");
  }

  for (let i = 0; i < this.allEvents.length; i++) {
    var timeStart = new Date(this.allEvents[i].start).getHours();
    var timeEnd = new Date(this.allEvents[i].end).getHours();
    var hourDiff = timeEnd - timeStart;

    this.exportData.events[i] = {
      start: this.allEvents[i].start,
      end: this.allEvents[i].end,
      title: this.allEvents[i].title,
      userid: this.allEvents[i].userId,
      hours: hourDiff,
    };
  }

  this.exportData.title = "Report for " + item.firstName + " " + item.lastName + " for dates in range " + this.dateRangeText;
  this.exportData.fileName = item.firstName + " " + item.lastName + " - " + this.dateRangeText;
  this.exportData.worksheetName = item.firstName + " " + item.lastName + " " + this.dateRangeText;
},

但是,我将数据发送到 (exportData.events) 的数组在我单击该按钮两次之前不会填充。我正在为 allEvents 使用 getter,并且承诺已完成。正如您在下面看到的,第一次实现了承诺,但是 for 循环中的数据不会返回到空数组中。任何建议将不胜感激。

商店:

const state = {
    events: []
};

    const actions = {
        getAllByFilter({ commit }, exportObj) {
        calendarService.getAllByFilter(exportObj)
            .then(
                events => commit('getAllSuccess', events),
                error => commit('getAllFailure', error)
            );
        },
    };
    
    const getters = {
    allEvents: state => state.events
    };
    
    const mutations = {
    getAllSuccess(state, events) {
        state.events = events;
        },
    };
    
    export const calendar = {
        namespaced: true,
        state,
        actions,
        mutations,
        getters
    };

【问题讨论】:

  • 发送您的商店的操作、突变和状态。填充 exportData.event 取决于 allEvent getter。您在哪里更新基础数据?
  • @Mr.您好,感谢您的评论!你相信这家店是这样的吗?我现在已将其添加到代码中。
  • 作为一种解决方法,我可以使用状态(事件:(状态)=> state.calendar.events)。但是,这不会让我像 getter 那样处理状态(在方法中)。

标签: javascript vue.js vuex


【解决方案1】:

问题出在这里:

this.getAllByFilter(this.exportObj);

此操作中的 Promise 未在任何地方处理。返回你的 Promise 然后等待它。

const actions = {
    getAllByFilter({ commit }, exportObj) {
    return calendarService.getAllByFilter(exportObj)
        .then(
            events => commit('getAllSuccess', events),
            error => commit('getAllFailure', error)
        );
    },
};

将您的导出方法修改为:

async Export (item) {
  this.exportObj = {
    start: this.dates[0],
    end: this.dates[1],
    userid: item.id,
  };

  await this.getAllByFilter(this.exportObj);
  ....

在 Export 方法完成其工作之前显示进度条、加载对话框或禁用按钮。

【讨论】:

  • 赞赏,但这里不是答案。。promise在calendarService.getAllByFilter函数中解析和处理,因此无需在商店中返回。我确实相信这与“Vue 反应性规则”有关,因为我可以更改突变中的代码,它会返回正确的结果。我通常也处理带有请求状态的承诺,但到目前为止还没有因为我试图调试它......我不认为需要使方法异步?
  • 您需要等待 getAllByFilter 方法,因为您的其余代码都依赖于它。
猜你喜欢
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
相关资源
最近更新 更多