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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With