首页 > 解决方案 > VueJS 函数与 then

问题描述

我有以下方法:

    getData(id){
          this.$store.dispatch('getData', {
            employeeId: this.employeeId
          }).then(() => {
            if (this.employeeData.length) {
              // here some code...
            }
          });
        },
 selectEmployee(d) {

      this.getData(d.employeeId);

      // I need to execute this only after getData has been processed...
      this.$store.dispatch('fetchHistory');
    },

所以我想做的是这样的:

selectEmployee(d) {

          this.getData(d.employeeId).then(() => {
             // check data here and then execute the fetch
             this.$store.dispatch('fetchHistory');
          });

        },

但是我在thensecuence 中遇到错误,那里似乎是不允许的。

有什么线索吗?

标签: vue.js

解决方案


返回中的承诺getData,然后您可以执行以下操作getData().then()

getData(id){
  return this.$store.dispatch('getData', {
            employeeId: this.employeeId
         }).then(() => {
           if (this.employeeData.length) {
              // here some code...
           }
         });
}

推荐阅读