Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Activated Route

Tags:

angular

router

Maybe I am missing something?

In an Angular 2 component, I am simply trying to get the full Activated Route.

I do not want to use location.href.

My constructor is as follows:

constructor(private route: ActivatedRoute) {}

And in ngOnInit() I tried finding the value here to no avail:

this.route.data.url
like image 870
DevMike Avatar asked Jun 30 '17 19:06

DevMike


2 Answers

You don't need to use ActivatedRoute for that, you need to use Router to query for the current URL in the form of string:

constructor(r: Router) {
   console.log(r.url);
}
like image 176
Max Koretskyi Avatar answered Nov 11 '22 21:11

Max Koretskyi


a simpler solution is to use LocationStrategy class from the @angular/common for representing and reading route state directly from the browser's URL which is more convenient then this.router.url when trying to get the router URL in form of a string.

  import {LocationStrategy} from '@angular/common';


export class MyComponent implements OnInit {

    constructor(private url:LocationStrategy) { }

    ngOnInit() {
          //this will log the current url
        console.log(this.url.path());
    }

}
like image 39
Hamed Baatour Avatar answered Nov 11 '22 21:11

Hamed Baatour