![](/img/trans.png)
[英]how to wait for http request for returning response with in subscribe method in angular 8?
[英]How to wait for HTTP Request Response + Confirm Dialogue PopUp + Angular 8
我有一个要求,我想在重定向到其他页面之前显示确认弹出消息,在那里我从用户天气中获取输入,他想离开是否有未保存的更改。
怎么了!
在浏览器上确认对话框首先出现,然后得到不正确的 HTTP-Request 响应。
应该是什么!
我希望首先获得 HTTP 响应,如果条件满足则弹出确认对话消息。
这是我用于我的项目的代码示例。
service.ts文件
async IsUnSavedRosterExistsTwo() {
console.log('async method called');
const url = this.util.getBaseURL() + apiURLs.IsUnSavedRosterExists;
const rr = await this.http.get<boolean>(url).toPromise();
console.log(rr);
return rr;
}
我已经尝试了所有可能的解决方案,例如 subscribe、promise、pipe,但这里没有任何工作。
组件.ts
canDeactivate() {
this.saveDownloadButton = false;
// const IsUnSavedRosterExists = this.rosterService.IsUnSavedRosterExists();
const IsUnSavedRosterExists = this.rosterService.IsUnSavedRosterExistsTwo();
debugger;
if (IsUnSavedRosterExists) {
console.log('executing the code');
return confirm('You have unsaved Employee Data. Do you wish to proceed without saving?');
} else {
this.saveDownloadButton = false;
}
return this.saveDownloadButton;
}
简而言之,我想等待http-request响应首先出现,然后如果条件满足则执行然后弹出确认对话框,否则重定向到其他页面。
如果我的问题不清楚,请告诉我。
您必须将canDeactivate()函数转换为异步函数,然后您可以等待this.rosterService.IsUnSavedRosterExistsTwo()函数解决,然后再继续。 请参阅下面的代码:
async canDeactivate() {
this.saveDownloadButton = false;
const IsUnSavedRosterExists = await this.rosterService.IsUnSavedRosterExistsTwo();
debugger;
if (IsUnSavedRosterExists) {
console.log('executing the code');
return confirm('You have unsaved Employee Data. Do you wish to proceed without saving?');
} else {
this.saveDownloadButton = false;
}
return this.saveDownloadButton;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.