前端方法执行之后在执行某操作,处理办法

场景

需要获取数组的最后一个,结果获取时,数组还没有更新到最新,导致只能获取到倒数第二个

处理办法

在方法内使用 return new Promise ,并在调用方法后 .then ,即可实现方法执行后在执行

/** 查询项目单体列表 */
getList() {
   this.loading = true;
   const projectId = this.$store.state.project.currentProject.projectId;

	//1.对方法进行改造
   return new Promise((resolve, reject) => {
     allBuild(projectId).then(response => {
       this.buildList = response.data;

       resolve(this.buildList) //2. 执行成功返回
     }).catch(err => {
       console.error(err);
     }).finally(() => {
       this.loading = false;
     });
   })
 },


addBuild(this.form).then(response => {
  this.msgSuccess("新增成功");
  this.open = false;
  this.isDisabled = false;
  // 3. 需要让列表获取后,在执行操作
  this.getList().then(list => {
    //4.执行要处理的逻辑:滚动到最新一个
    var buildId = this.buildList[this.buildList.length - 1].buildId;
    this.scrollToView(`#build_${buildId}`);
  });
  
}).catch(() => {
  this.isDisabled = false;
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值