How to get current route
The new V3 router has a url property.
this.router.url === '/login'
Angular RC4:
You can import Router
from @angular/router
Then inject it:
constructor(private router: Router ) {
}
Then call it's URL parameter:
console.log(this.router.url); // /routename
Inject Location
to your component and read location.path();
You need to add You need to add ROUTER_DIRECTIVES
somewhere so Angular can resolve Location
.import: [RouterModule]
to the module.
Update
In the V3 (RC.3) router you can inject ActivatedRoute
and access more details using its snapshot
property.
constructor(private route:ActivatedRoute) {
console.log(route);
}
or
constructor(private router:Router) {
router.events.subscribe(...);
}
See also Angular 2 router event listener
Use this
import { Router, NavigationEnd } from '@angular/router';
constructor(private router: Router) {
router.events.filter(event => event instanceof NavigationEnd)
.subscribe(event => {
console.log(event);
});
}
And in main.ts
import
import 'rxjs/add/operator/filter';
EDIT
Modern way
import {filter} from 'rxjs/operators';
router.events.pipe(
filter(event => event instanceof NavigationEnd)
)
.subscribe(event => {
console.log(event);
});