I have a navbar component that is called in every pages of my website.
I want it to check when the URL change with a HostListener.
@HostListener('window:hashchange', ['$event'])
onHashChange(event) {
this.checkCurrentURL();
}
private checkCurrentURL(){
console.log("loaction : "+window.location.pathname)
}
However it doesn't work. Any ideas ?
EDIT: SOLUTION
The solution I found is without HostListener, but it works.
constructor(private router : Router){
router.events.subscribe((val) => {
this.checkCurrentURL();
});
}
You don't need a HostListener for this. Assuming you are developing a single page app, you wanna subscribe to route change event.
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