【Jeecg Vue】通过getAction的finally来最大程度避免影响主数据呈现

2022-10-08,,,,

  

企业信息列表,查看某条记录时,弹窗页里要求展示企业的用户名,而用户名字段不在企业表里。

为此,我们需要修改弹窗页的渲染方法。

methods: {

  enterpriseinfo (record) {
    this.form.resetfields();
    let product;
    if(record.product == 'huichuxing'){
      product = '惠出行';
    }else if(record.product == 'bosskg'){
      product = 'boss开工';
    }else if(record.product == 'huichuxing,bosskg' || record.product == 'bosskg,huichuxing'){
      product = '惠出行,boss开工';
    }else{
      product = '业务未开通';
    }
    this.model = object.assign({}, record);
    this.model.product = product;
    this.visible = true;
    this.$nexttick(() => {
      this.form.setfieldsvalue(pick(this.model,'enterprisename','address','legalname','email','phone','username','licensepic',
        'iplist','taxpayerno','billaddress','bankname','bankcardno','billphone','product'));
    });
  }
}

 

我的想法很清晰,recoord代表的是指定的企业的信息,在this.visible = true;前,给this.model.username属性重新赋值。

 

服务端接口很快实现了。不过,修改这个vue页面的时候倒是吃力了。对于jeecg-boot 的这套前段ui框架ant design jeecg vue,虽然已经跟了几个项目了,依然是一知半解。

自然是通过getaction来赋值了,然并卵。因为getaction是异步请求,所以,肯定是不起作用了。

那么,该怎么办呢?

 

一个小伙给出了方案,当然,这也是我不得已而为之的方案————在getaction请求成功的方法里,给username赋值,然后,再进行页面渲染。

methods: {

  enterpriseinfo (record) {
    ......
    this.visible = true;
    getaction('/ent/getenterpriseloginacc?enterpriseid=' + record.enterpriseid).then(response => {
      record.username = response.result.loginacc;
      this.model = object.assign({}, record);
      this.$nexttick(() => {
        this.form.setfieldsvalue(pick(this.model, 'enterprisename', 'address', 'legalname', 'email', 'phone', 'username', 'licensepic', 'taxpayerno', 'billaddress', 'bankname', 'bankcardno', 'billphone', 'product', 'industrytype1', 'industrytype2'));
      });
    });
    
  }
}

 这样虽然实现了,但与我的想法有些不一致。怎么讲?假如说,执行getaction出问题,那么整个页面将无法呈现。这岂不因小失大!

 

后来,问一个前端的同事,终于指点了迷津。 同事发的如下这个示意图,提示可在1处、2处做文章。

 

 

当然,经过尝试,发现类似getaction\postaction\putaction除了.then()、.catch()外,还有finally()。那看来,在没有其他方案的情况下,————也许没有其他方案了————这是最好的方案了,也符合我的期望。

methods: {

  enterpriseinfo (record) {
    ......
    this.visible = true;
    this.$nexttick(() => {
      getaction('/ent/getenterpriseloginacc?enterpriseid='+record.enterpriseid).then(res => {
        if(res.success) {
          this.model.username = res.result.loginacc;
        }
      }).finally(() => {
        // console.log("username= "+this.model.username)
        this.form.setfieldsvalue(pick(this.model,'enterprisename','address','legalname','email','phone','username','licensepic',
          'iplist','taxpayerno','billaddress','bankname','bankcardno','billphone','product','industrytype1','industrytype2'));

      });

    });
    
  }
}

 

《【Jeecg Vue】通过getAction的finally来最大程度避免影响主数据呈现.doc》

下载本文的Word格式文档,以方便收藏与打印。