0

I need to intercept the page reload when the user clicks the reload button in the browser. I tried the following code but my break point never pops up.

ngOnInit() {
    this.router
        .events
        .subscribe((e: RouterEvent) => {
            debugger;
        });
}

Can anyone help please?

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
Antediluvian
  • 653
  • 1
  • 6
  • 18
  • 1
    Your application restarts from scratch when the reload button is clicked. So your root component's constructor and ngOnInit method is called, for example. What concrete problem are you trying to solve? – JB Nizet Aug 24 '19 at 11:15
  • I think this question has already been answered here: https://stackoverflow.com/questions/47331260/how-to-prevent-page-refresh-in-angular-4 – chrismclarke Aug 24 '19 at 12:04
  • @JBNizet I have a child component which shows up based on some conditions. At some point, if this child shows up but the user clicks the reload button, I would still wanna bring up this child. – Antediluvian Aug 26 '19 at 01:34
  • Then make sure the condition is based on the value of the URL. If the user reloads, the app will display the route associated to that URL, and your child component will be redispayed. – JB Nizet Aug 26 '19 at 05:54
  • @JBNizet That will need a route and an outlet. But this child needs the whole of the viewport while its parent is a 2-column layout. That is why I have to have some conditions to change the layout. Otherwise I will need a route for this child the same level as its parent. – Antediluvian Aug 30 '19 at 01:07

1 Answers1

0

You can use

@HostListener("window:beforeunload")

Like this

@HostListener("window:beforeunload")
public doSomething() {
  // custom logic here
}
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60