Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to elegantly get full url from the ActivatedRouteSnapshot?

In some of my Angular route guards, I want to set up the "next" path, to redirect to after successful login.

So, the ordinary guard canActivate function signature looks like this:

public canActivate(route: ActivatedRouteSnapshot): Observable<boolean> | boolean {   // ...blah   return true; } 

The route parameter is an instance of ActivatedRouteSnapshot.

Previously to get the "next" URL I was just getting it from the route.url. This works just fine, as long as there are no children routes.

My example URL is /search/advanced?query_hash=1221d3b57f5616ee16ce70fdc78907ab, where advanced is a child route of a search.

Child routes can be found in route.children, but iterating over these children (especially there might be multiple levels) and combining the URL this way seems awkward and ugly.

What I'm interested in is contained in route._routerState.url property (being a string, on the bottom of the image below), but it's a "private" variable.

Am I missing something? How can one elegantly get the full (with children paths) URL from the ActivatedRouteSnapshot? Angular version is 5.1.

enter image description here

like image 922
Radosław Roszkowiak Avatar asked May 09 '18 09:05

Radosław Roszkowiak


People also ask

How do I find the URL of a Activated route?

Steps to get current route URL in Angular. Import Router,NavigationEnd from '@angular/router' and inject in the constructor. Subscribe to the NavigationEnd event of the router. Get the current route url by accessing NavigationEnd's url property.

What is the difference between ActivatedRoute and ActivatedRouteSnapshot?

Since ActivatedRoute can be reused, ActivatedRouteSnapshot is an immutable object representing a particular version of ActivatedRoute . It exposes all the same properties as ActivatedRoute as plain values, while ActivatedRoute exposes them as observables.

What is activated route snapshot?

ActivatedRouteSnapshotlinkContains the information about a route associated with a component loaded in an outlet at a particular moment in time. ActivatedRouteSnapshot can also be used to traverse the router state tree.

Can you activate guard?

What is CanActivate Guard. The Angular CanActivate guard decides, if a route can be activated ( or component gets rendered). We use this guard, when we want to check on some condition, before activating the component or showing it to the user.


1 Answers

There's no ready to use function from Angular router to achieve that, so I wrote them:

function getResolvedUrl(route: ActivatedRouteSnapshot): string {     return route.pathFromRoot         .map(v => v.url.map(segment => segment.toString()).join('/'))         .join('/'); }  function getConfiguredUrl(route: ActivatedRouteSnapshot): string {     return '/' + route.pathFromRoot         .filter(v => v.routeConfig)         .map(v => v.routeConfig!.path)         .join('/'); } 

Example output when route is from ProjectComponent:

const routes: Routes = [     {         path: 'project', component: ProjectListComponent, children: [             {path: ':id', component: ProjectComponent}         ]     }, ]; getResolvedUrl(route) => /project/id1 getConfiguredUrl(route) => /project/:id 
like image 137
Marc J. Schmidt Avatar answered Sep 27 '22 23:09

Marc J. Schmidt