Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 get ActivatedRoute url

Tags:

I want to get the url which comes from this:

this.router.navigate(./somepath, { relativeTo: this.route })

this.route is of the type ActivatedRoute.

I tried url: string = route.snapshot.url.join(''); but this gives an empty string.

like image 914
Robin Dijkhof Avatar asked Oct 20 '16 11:10

Robin Dijkhof


People also ask

How do I get the URL for ActivatedRoute?

You can use the activated route to get active URL. (<RouterStateSnapshot>activatedRoute['_routerState']). url , if you like to type it.

What is ActivatedRoute Angular?

What is an activated route? The Angular Docs define the ActivatedRoute as. A service that is provided to each route component that contains route specific information such as route parameters, static data, resolve data, global query params and the global fragment.


2 Answers

This should help:

constructor(router: Router) {   const url = router.url; } 
like image 86
Gabriel Martins Avatar answered Sep 24 '22 21:09

Gabriel Martins


You can use the activated route to get active URL.

import { ActivatedRoute } from '@angular/router';  constructor(     private activatedRoute: ActivatedRoute) {     console.log(activatedRoute.snapshot['_routerState'].url);  } 
like image 21
viks Avatar answered Sep 25 '22 21:09

viks