使用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();