1

I'm using active class in my router link like this :

<li class="list-group-item">
  <router-link :to="{name:'category'}" active-class="active">
    Category
  </router-link>
</li>

this code is working and the link is active but i want this link to be active on two router link i.e. on category and subcategory how can i achieve this result.

3
  • Is subcategory a child route of category? Commented Nov 30, 2020 at 5:25
  • @Phil: No it is not a child route Commented Nov 30, 2020 at 5:27
  • Try using linkExactActiveClass of vue router link Commented Nov 30, 2020 at 6:08

1 Answer 1

1

Given your subcategory route is not a child of category, you'll have to implement the logic yourself

<router-link
  :to="{ name: 'category' }"
  active-class="active"
  :class="{
    active: $route.matched.some(({ name }) => name === 'subcategory')
  }"
>
  Category
</router-link>

This will add the active class if the route matches category (or any of its children) as well as subcateory (or any of its children).

Edit icy-smoke-he1sd

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.