Vue-router: helper for highlighting current active link

Created on 8 Jul 2015  ·  14Comments  ·  Source: vuejs/vue-router

similar to ui-router's ui-sref-active

feature

Most helpful comment

0e11456d33a9dd3cb0fbd9c146ac935063ebfe75

For ease of use: the active link will automatically get .v-link-active. This class name can be configured as an option when creating the router as linkActiveClass.

All 14 comments

I was doing like v-class="active: route.path=='/page'" :) but helper would make it elegant probably. Also, something like route.componentName would be useful, i find myself doing route.path.split('/')[n] many times to find out which sub-route is currently active. What do you think?

@azamat-sharapov :+1:

0e11456d33a9dd3cb0fbd9c146ac935063ebfe75

For ease of use: the active link will automatically get .v-link-active. This class name can be configured as an option when creating the router as linkActiveClass.

Oh yeah, now that's really Evan. :+1:

Would it make sense to have an option to also have the class present of a child route is active?

For example, if I have a 'users' link in my nav, I might want that to also be highlighted when I'm on 'users/:id' or 'users/create'.

@JosephSilber I'll think about that, but it would introduce additional api surface to configure this behavior. In those cases it's probably better to do something csutom like v-class="active: /^user/.test(route.path)", or even a custom directive.

Ah, actually, we can just add an additional auto class: .v-link-child-active...

Or, maybe a partial match should be the default, where exact match adds the class .v-link-active-exact

Have you any idea why the classes do not apply on the app first init ?

Then when I navigate it updates itself!

@manudurgoni not sure, it seems to be working correctly in the example (on initial load with a matched url).

@yyx990803 You've right :)

On initial load, my route path is : fr/categorie/slug, so it doesn't match with the destination who is : /fr/categorie/slug. And when I navigate, all is good. I just need to find why the first slash is removed on init.

EDIT : ok I fixed my code :)

@yyx990803 Any plans to include something like the .v-link-active-exact you proposed earlier?

@amirrustam FYI in 2.0 there's exact matching mode: http://router.vuejs.org/en/api/router-link.html

@fnlctrl 🙏 Thank you. I read right passed that when reading the docs.

As 2.5.0+, Now just :
<router-link to="/" exact-active-class="is-active">Home</router-link>

Thanks @fnlctrl

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Atinux picture Atinux  ·  3Comments

posva picture posva  ·  3Comments

Lyfme picture Lyfme  ·  3Comments

shinygang picture shinygang  ·  3Comments

saman picture saman  ·  3Comments