Angular 8 beforeunload事件事件在关闭选项卡上不起作用 [英] Angular 8 beforeunload event event not work on close tab

查看:49
本文介绍了Angular 8 beforeunload事件事件在关闭选项卡上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我尝试在关闭标签上发出http请求,但它不起作用,但是,如果我导航到其他组件,它的工作.我尝试更改类似此帖子的功能 Angular 2-执行代码关闭窗口时

Hey i try to make http request on close tab but its not work, But if i navigate to other component its work. I try to change the function like this post Angular 2 - Execute code when closing window

但是它现在对我有用.

我的http服务:

  removeCurrentEditor(reportID) {
    return this.http.request('GET', this.baseUrl + this.REPORTS_API + '/' + this.REMOVE_CURRENT_EDITOR
     + '/' + reportID, {responseType: 'json'});
  }

我的组件:

 // Handle close tab
  @HostListener('window:beforeunload', ['$event'])
  beforeUnloadHander(event) {
    this.reportService.removeCurrentEditor(this.reportID);
  }

谢谢!

推荐答案

我们的应用程序存在类似的问题,我们必须在浏览器选项卡关闭时发出http调用.问题是关闭浏览器后,它将取消所有xhr调用.因此,没有办法实现这一点,Angular不支持开箱即用的同步http调用.

We had a similar issue with our application where we had to issue a http call on close of browser tab. The problem is when the browser is closed, it cancels all xhr calls. Hence there was no way to achieve this and angular does not support syncronous http calls out of the box.

最后,我们最终在onBeforeUnload事件中使用了同步的xmlhttprequest.这样,浏览器会在关闭浏览器之前等待调用完成.

Finally, we ended up using a synchronous xmlhttprequest in the onBeforeUnload event. This way, the browser waits for the call to complete before closing the browser.

XMLHttpRequest.open(method, url[, async[, user[, password]]])

如果async参数设置为false,则表示同步请求.

The async parameter if set to false indicates a synchronous request.

您可以探索的另一个选项是诸如心跳客户端之类的东西,它可以检测应用程序是否处于打开状态.如果没有,服务器可以执行逻辑.

Another option you could explore is something like a heartbeat client which could detect if the application is open. If not, the server could perform the logic.

这篇关于Angular 8 beforeunload事件事件在关闭选项卡上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆