Home  >  Q&A  >  body text

How to make Vue Router add 'link-exact-active-class' at the end of class list?

I tried using the linkExactActiveClass parameter in the router constructor to highlight active links in the navigation bar.

My problem is that the active class is added to the beginning of the css class list, which means other classes override it.

This is my router configuration:

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  linkExactActiveClass: 'text-yellow-500',
});

router-link The elements look like this:

<li>
    <router-link class="px-2 text-white" to="/manage">Manage</router-link>
</li>

question:

text-yellow-500 classes are indeed added before existing css classes. This causes the yellow text to be overwritten by the text-white class. How can I change this?

P粉193307465P粉193307465236 days ago380

reply all(1)I'll reply

  • P粉517475670

    P粉5174756702024-02-26 00:46:20

    A simple solution is to use tailwind (documentation) in the class

    Try using it like this:

    const router = createRouter({
      history: createWebHistory(process.env.BASE_URL),
      routes,
      linkExactActiveClass: '!text-yellow-500',
    });
    

    Normally, I don't like this approach, but I don't think there is any other easy option

    PS: Make sure you are using at least tailwind 2.1 and JIT mode

    reply
    0
  • Cancelreply