【问题标题】:Need to call logout call on browser close - In angular需要在浏览器关闭时调用注销调用 - 角度
【发布时间】:2020-11-07 17:35:29
【问题描述】:

以下是我的注销电话:

logoutClick() {
    let response: any;
    this._httpService.fetch(SERVICE_URL.LOGOUT, 'get').subscribe(
        (res: any) => {
            response = res;
        },
        (error: any) => {
            this._commonService.showLoader = false;
            this._cookieService.delete('tokenId');
            this._cookieService.delete('userInfo');
        },
        () => {
            debugger;
            console.log('abgffgdfgdg');
            this._cookieService.delete('tokenId');
            this._cookieService.delete('userInfo');
            this._commonService.showLoader = false;
        },
    );
}

这需要在浏览器关闭时调用,

在下面尝试过:

window.addEventListener('beforeunload', function (e) {
    e.preventDefault();
    this.onLogoutClick()
});

没有按预期工作, 使用角度版本 7

【问题讨论】:

  • 1) that.onLogoutClick() 错字:that. -> this. 2) 如果“完成”,订阅中的第三个参数将运行。

标签: angular asynchronous browser rxjs angular7


【解决方案1】:

看起来您正在使用 Angular httpClient 请求,这是一个异步请求。

您不能在浏览器关闭事件中执行异步事件。

尝试使用同步请求:

1)XMLHttpRequest

注意:最新的 Chrome 现在不支持了 - Read here

2)异步/等待

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 2020-10-23
    • 2017-01-05
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2017-12-28
    相关资源
    最近更新 更多