Skip to content

Retrieve all route segments configuration by URL #15750

@smnbbrv

Description

@smnbbrv

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

There is no clear way to get the configuration, see http://stackoverflow.com/q/43143621/1990451.

Given a URL, e.g. /path/path2/path3/123;sdf=df and a routes configuration:

{ path: 'path', data: { g: 'h' }, children: [
  { path: 'path2', data: { c: 'd' }, children: [
    { path: 'something', component: TestpageComponent, data: { a: 'b' } },
    { path: 'path3/create', component: TestpageComponent, data: { b: 'c' } },
    { path: 'path3/:id', component: TestpageComponent, data: { b: 'c' } },
  ] }
] },

It would be useful to find an actual configuration for all the segments in order to e.g. get a result set of all data parameters on all levels (each segment of the url).

It is possible to split the URL to the segments by e.g.

console.log(this.router.parseUrl(url));

or to be more precise

console.log(this.router.parseUrl(url).root.children['primary'].segments);

which will return

[{"path":"path","parameters":{}},{"path":"path2","parameters":{}},{"path":"and","parameters":{}},{"path":"123","parameters":{"sdf":"df"}}]

So, splitting the URL into segments is not a problem. However it does not provide the configuration for each segment. One can get the actual configuration for all the routes with

console.log(this.router.config);

It's possible to go with the parsed segments through the configuration tree and resolve the branch (and it's not that complicated actually), but it can cause troubles e.g. while resolving :id against create segment. So, it would be cool to use the router's way of resolving the configuration.

Expected behavior

I would expect a method on a Router service that returns an array of all the configurations available for a given URL. E.g.

router.configurationByUrl('/path/path2/path3/123;sdf=df')
// returns [
//  { path: 'path', data: { g: 'h' }, children: [...] },
//  { path: 'path2', data: { c: 'd' }, children: [...] },
//  { path: 'path3/:id', component: TestpageComponent, data: { b: 'c' } }
//], 

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Imagine I have a 50% of the URLs protected by some guard (e.g. only logged in users could go there) and other 50% is not protected (displayed for everybody). So, on the navigation menu level (outside of the router-outlet) I want to display only the links which are relevant for the current user, so I need to know which routes are protected by the guard. Guards / data / whatever is just a particular case of the problem.

Please tell us about your environment:

  • Angular version: 2.0.X / 4.0.X
  • Browser: [all]
  • Language: [all]

  • Node (for AoT issues): node --version = X

Metadata

Metadata

Assignees

Labels

area: routerfeatureLabel used to distinguish feature request from other issues

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions