Hy everone! Can anyone tell me how to invoke an angular 7 function after each page load?
You can subscribe to the router events
in the first page you load (usually app.components.ts
) and check if the instance of the event is a NavigationEnd
.
To do so you can have to do this:
import { Router,NavigationEnd } from '@angular/router';
constructor(
private router: Router
) {
this.router.events.subscribe((e) => {
if (e instanceof NavigationEnd) {
// Function you want to call here
}
});
}
You have the live example here:
https://stackblitz.com/edit/angular-routing-function-stack-55723837?file=src/app/app.component.ts
you can do that in the ngOnInit() method of the app component.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With