How to know the current route in Angular 2

4,356 views
Skip to first unread message

Ping Chen

unread,
Oct 29, 2015, 12:35:21 AM10/29/15
to AngularJS
I have defined some routes in AppComponent. It works fine, I can change route by clicking router-links.

But in AppComponent, I want to find out the current route I'm in, don't know how.

Thanks.

Wolfgang Stöttinger

unread,
Nov 29, 2015, 2:25:24 PM11/29/15
to AngularJS
+1

Eric Martinez

unread,
Nov 29, 2015, 7:40:19 PM11/29/15
to AngularJS
Just subscribe to the router

constructor(router: Router) {
 
     router
.subscribe((url) => console.log(url));

}

That URL will hold the path of the route you are in. If you want the Instruction, complement it with "recognize" :

constructor(router: Router) {
 
     router
.subscribe((url) => {
          
          // Current URL

          router.recognize(url).then((instruction) => {
              
             console.log(instruction); // Current instruction

          });

     });



}



Wolfgang Stöttinger

unread,
Nov 30, 2015, 5:25:17 AM11/30/15
to AngularJS
works like a charm! thanks! 

Josh Evenson

unread,
Jan 13, 2016, 3:08:07 PM1/13/16
to AngularJS
This doesn't seem to be working with angular 2.0.0-beta.0
Reply all
Reply to author
Forward
0 new messages