我有一个utils类,它包含showMsg函数来使用ui/dialogs
showMsg(titleTxt: string, msg: string, btnTxt: string, type: string){
switch(type){
case 'confirm':
dialogs.confirm({
title: titleTxt,
message: msg,
okButtonText: btnTxt,
cancelButtonText: 'No'
}).then((result) => {
console.log(titleTxt + " Dialog closed!" + result);
return result;
});
break;
default:
dialogs.alert({
title: titleTxt,
message: msg,
okButtonText: btnTxt
}).then(() => {
console.log(titleTxt + " Dialog closed!");
return true;
});
}
}
问题是,当我从另一个组件调用此函数以进行确认对话框时,该组件在继续之前不会等待对话框的结果。我希望它根据用户响应有条件地继续。
编辑:
新的showMsg函数
showMsg(titleTxt: string, msg: string, btnTxt: string, type: string){
switch(type.toLowerCase()){
case 'confirm':
return dialogs.confirm({
title: titleTxt,
message: msg,
okButtonText: btnTxt,
cancelButtonText: 'No'
}).then((result) => {
console.log(titleTxt + " Dialog closed!" + result);
return result;
});
default:
return dialogs.alert({
title: titleTxt,
message: msg,
okButtonText: btnTxt
}).then(() => {
console.log(titleTxt + " Dialog closed!");
return true;
});
}
}
现在对showMsg函数的调用如下
this.myutil.showMsg('Update Cleaning Status', 'Are you Sure?', 'Yes', 'Confirm').then((result)=> {
console.log('In then: ' + result);
alert(result);
});
不幸的是,在调用函数之后,我们不能有任何代码,因为它在中发生的事情之前执行。然后()