Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v-link-active only works on direct parent #377

Closed
BenRomberg opened this issue Feb 19, 2016 · 8 comments
Closed

v-link-active only works on direct parent #377

BenRomberg opened this issue Feb 19, 2016 · 8 comments

Comments

@BenRomberg
Copy link

I'm trying to use the new v-link-active attribute and it only seems to work on the direct parent. I'd need to use it nested in a <li v-link-active><ul><li><a v-link="..."> scenario.

The documentation hints that this should work: "v-link will locate the closest parent element that has v-link-active and apply the active classes on that element instead."

Also, I have a use-case where multiple v-link-active would prove useful, e.g. <li v-link-active><ul><li v-link-active><a v-link="...">. Would be awesome if that would work as well.

@svedova
Copy link

svedova commented Mar 10, 2016

Same here.

It would be useful to keep a class on parent's parents. When you have nested routes the elements which link to parent route should have a supplementary class (v-link-active-trail) for instance. Would be useful...

@cnicodeme
Copy link

More over, it would be interesting, in certain case, to work on multiple v-link-active (for example for nested sub-menus, when we need to select the current active element + show the parents, and parents element.

@azamat-sharapov
Copy link

It would probably have performance issues then, because it needs to go all the way up to find v-link-active. No?

@cnicodeme
Copy link

In my opinion I would say it's quite fast since it knows where to go (any dom element has only one parent up to body). But it's my opinion, not the absolute truth ;)

I made a quick & easy fix, simply put : v-bind:class="{ 'active': hasRoute('/my/url') }" in my <template /> and the hasRoute simply do an indexOf between it's argument and the current url (from $router)

Works like a charm. Here's the code :

hasRoute: function (partial) {
    return (this.$route.path.indexOf(partial) > -1)
}

This let me place the active class anywhere I want.

@yyx990803
Copy link
Member

Cannot reproduce this, can you provide a jsfiddle?

@yyx990803
Copy link
Member

Hmm let me guess - are you having something like this?

<li v-link-active>
  <ul>
    <li v-for="xxx" v-link="xxx">xxx</li>
  </ul>
</li>

@tejitak
Copy link
Contributor

tejitak commented Apr 5, 2016

@yyx990803 I guess so. Here is a working example.
https://jsfiddle.net/tejitak/o44z47ag/5/

Currently each link directive listens route change event and update the associated v-link-active element based on matching with the current route and the link value.
It does not work when multiple link directives have a same v-link-active parent because the updates triggered on last v-link directive overrides the v-link-active.

How about making either of the following changes?

  1. Just use nextTick for adding a v-link-active class (It's simple solution but not desirable)
  2. Listen route change event in v-link-active directive and check if one of v-link children is matched

@yyx990803
Copy link
Member

Fixed in 3d12e95 (multiple v-link-active is now also supported)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants