【Promise】某个异步方法执行结束后 在执行下面方法


使用Promise ,当 layer.msg('查询成功') 这个方法执行结束后 ,下面代码才会执行


let thas = this
async function showMessage() {
    await new Promise(resolve => layer.msg('查询成功', resolve));
    // 这里的代码将在 layer.msg 执行结束后执行
    thas.isGuarantee = true;
    thas.InsurancePolicyInfo = res.data;
    thas.ifTimeClose();
}
showMessage();


注意事项:Promise内如果使用 this , this无法访问到Vue实例 ;
解决方法:

1.你需要在外部定义全局变量 值是this, 在promise内使用这个变量


2.直接调用Vue组件实例
描述:箭头函数不会改变 this 的指向,它会捕获外部上下文的 this。因此,在箭头函数内部,this 会指向 Vue 组件的实例

async showMessage() {
  await new Promise(resolve => layer.msg('查询成功', resolve));
  this.isGuarantee = true;
  this.InsurancePolicyInfo = res.data;
  this.ifTimeClose();
}

// 在 Vue 组件中调用 showMessage
this.showMessage();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值