在Angular4中关闭浏览器窗口之前如何调用API [英] How to call the API before closing the browser window in Angular4

查看:532
本文介绍了在Angular4中关闭浏览器窗口之前如何调用API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Angular 4应用程序中工作,在我的应用程序中,我有5个组件我想做的是,当用户从五个组件中的任何一个关闭浏览器窗口时,我需要调用API并说用户这次关闭了该窗口.

I am working in an Angular 4 application ,In my application I have 5 components What I am trying to do is ,When the user close the browser window from any one of the five components I need to call the API and say the user is closed the window on this time .

在我的情况下,关闭应用程序时未调用API.

In my case the API is not get called when I closed the application.

app.component.ts

app.component.ts

export class AppComponent implements OnInit {

constructor(private CartdataService: CartdataService, private http: HttpClient) { }

    ngOnInit() {}

    @HostListener('window:beforeunload', ['$event'])
        beforeunloadHandler(event) {
            this.WindowClosed();
        }

        WindowClosed() {

            this.CartdataService.get_DummyCall().subscribe();
        }
}

除了上面的代码,我这里没有实现任何其他功能.谁能告诉我我在哪里犯错了.

Here I haven't implemented anything other than the above code . Can anyone tell me Where I did the mistakes.

参考:

角度2-关闭窗口时执行代码

推荐答案

最后明白了...

@HostListener('window:beforeunload', ['$event'])
    public beforeunloadHandler($event) {
    this.CartdataService.get_DummyCall().subscribe();
   }

这篇关于在Angular4中关闭浏览器窗口之前如何调用API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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