Skip to content

[Router question/FR] How to expose route name? #5180

Closed

Description

@ericmartinezr

TL;DR

Is there a way to get the name property of a Route, or the complete info of it? If there isn't I think it would be super awesome to have it.

Long and boring story

I've asked in gitter's chatroom and after a brief brainstorm I couldn't get a definitive answer, so I'm asking here.

The use case for this is to create a breadcrumb where the name of the route would be extremely useful.

I have this simple route

{path: '/index', component: RoutingIndex, name : 'Index'}

And I want to get the name property so I can use it dynamically

class RoutingIndex {
   constructor(router: Router) {
       this.routeName = router.getThisParticularRouteName(); // Returns 'Index'
   }
}

This is just for a name property, but exposing all the info it would be nice too and you could name it RouteInfo or so and make it injectable and I totally came up with that name, I didn't steal it from anyone. Note that this one would be not the same as RouteData

Something like this

constructor(routeInfo: RouteInfo) {
     // Get the name for this particular route
     routeInfo.getName()
}

My workaround so far is this little piece of code

constructor(router: Router) {
    router.registry._rules.forEach((val) => {
        val.names.forEach((v, k) => console.log(v, k)); 
        // 'k' is the route name and 'v' is a lot of weird things
});

Not good, I'm iterating over something private, but it's one solution so far.

A shorter one would be this

{path: '/index', component: RoutingIndex, name : 'Index', data : {'name':'Index'}}

But that seems a little redundant and I would have to repeat it in every route and yes, I'm super lazy.

Any advice? Am I missing something obvious? Please don't say use the data property

Activity

  1. ericmartinezr commented on Nov 8, 2015

    @ericmartinezr
    ContributorAuthor

    @behzadeshan that seems like another good idea, but I'm not trying to retrieve the name from the URL. The user can write anything on it so it's not so safe. Besides that, it's not only about getting the path name but also getting the Route info, that's what I think getting the name from the Route would be safer. I'll keep in mind your suggestion, thanks 👍

    For now I'll stick to data property since the user can't modify it.

  2. angular-automatic-lock-bot commented on Sep 7, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  3. locked and limited conversation to collaborators on Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions