Search code examples
angularroutesrefresh

Detect browser refresh in an Angular project


I want to detect when a page is refreshed using a router in my single-page application (Angular project).

Is there an alternate way?


Solution

  • Try this:

      this.router.events
        .pipe(filter((rs): rs is NavigationEnd => rs instanceof NavigationEnd))
        .subscribe(event => {
          if (
            event.id === 1 &&
            event.url === event.urlAfterRedirects
          ) {
              // Your code here for when the page is refreshd
          }
        })
    

    You will need to import

    import { filter } from 'rxjs/operators'
    import { Router  } from '@angular/router';