4

I'm trying to get the Route object from passing a url.

interface Route {
    path?: string;
    pathMatch?: string;
    matcher?: UrlMatcher;
    component?: Type<any>;
    redirectTo?: string;
    outlet?: string;
    canActivate?: any[];
    canActivateChild?: any[];
    canDeactivate?: any[];
    canLoad?: any[];
    data?: Data;
    resolve?: ResolveData;
    children?: Routes;
    loadChildren?: LoadChildren;
    runGuardsAndResolvers?: RunGuardsAndResolvers;
}

In my navigation component I wish to access the Route information by passing a URL, as the route Object's data property contains my Access roles. I know that I can extract this Access role information to a map and the share the key between my Routes object and my Navigation object. But I was hoping to find some kind of why to traverse the Routes object and extract my access information from there.

This is what my Navigation object looks like:

{ path: '/maintenance/countries', name: 'Countries' }

So the path in this is passed to a routerLink in my Template. Thus what I'm looking for is a way to use that same path to get to the data as set up in the Routes object for that path. I can't just do a standard filter on the Routes object due to the fact that there are nested paths, so I suspect I need some way to use Angular's UrlMatcher.

Lastly, a fictitious example of what I was looking for:

const foundRoute: Route = this.router.findRoute('/maintenance/countries');
const hasAccess = this.authService.hasAccess(foundRoute.data.roles());
Lourens
  • 268
  • 3
  • 12

0 Answers0