Angular 2 Activated Route

Maybe I missed something ??

In the Angular 2 component, I'm just trying to get a fully activated route.

I do not want to use location.href .

my constructor is as follows:

 constructor(private route: ActivatedRoute) {} 

And in ngOnInit() I tried:

 this.route.data.url this.route.snapshot 

None of them work.

Any help is generally much appreciated.

+5
source share
2 answers

You do not need to use ActivatedRoute , you need to use Router to request the current URL in the form of a string:

 constructor(r: Router) { console.log(r.url); } 
+7
source

a simpler solution is to use the LocationStrategy class from @angular/common to represent and read the route state directly from the browser URL, which is more convenient than this.router.url when trying to get the router URL as 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()); } } 
+3
source

Source: https://habr.com/ru/post/1269386/


All Articles