I want to detect when a page is refreshed using a router in my single-page application (Angular project).
Is there an alternate way?
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';