【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();

猜你喜欢

转载自blog.csdn.net/m0_64494670/article/details/134608457
今日推荐