Posts

Showing posts from August, 2022

How to Detect Route Changes in Angular

In a current project at work, we need to be able to tell what the current route is in an Angular app regardless of which component is currently active. The code below does the trick. It's probably better suited for ngOnInit() than the constructor, but this still illustrates how to do it. In this example, we're just logging the URL to the console (this was just sample code I gave to one of the developers): export class AppComponent {   constructor ( private _router : Router ) {     this . _router . events . subscribe (( event : Event ) => {       if ( event instanceof NavigationEnd )         console . log ( "CURRENT URL: " , event . url );     });   } }