然后使用VueJS函数

时间:2018-07-24 19:20:56

标签: vue.js

我有以下方法:

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

        },

但是我在then安全中出现错误,似乎不允许这样做。

  

有任何线索吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

除了链接Promises之外,您还可以使用ES6异步等待,这使代码更清晰可读。

$: cat test2.txt
TM1ITP1-TMNLSTP1 SLC00=0,SLC01=0,SLC02=0,SLC03=0

$: sed 's/,/\nTM1ITP1-TMNLSTP1 /g' test2.txt
TM1ITP1-TMNLSTP1 SLC00=0
TM1ITP1-TMNLSTP1 SLC01=0
TM1ITP1-TMNLSTP1 SLC02=0
TM1ITP1-TMNLSTP1 SLC03=0