router-link-active not applied to matching routes

402 views Asked by At

I am currently in my Vue learning journey. I have come across a situation where I need to show my link as active for matching route too. For example: I have route /programs which shows Program link as active without any issues but I also want /programs/view to set Program link as active as well. How do I do it in Vue Router?

These are my route definitions in router.js file

const routes = [
    { path: '/', component: Main, name: 'main', redirect: '/dashboard', children:
    [
        { path: '/dashboard', component: Dashboard, name: 'dashboard' },
        { path: '/programs', component: Programs, name: 'programs' },
        { path: '/programs/view', component: ViewProgram, name: 'view_program'},
        ...OTHER ROUTES
    ]
    },
    { path: '/login', component: Login, name: 'login', meta: {noAuth: true} },
    { path: '/:pathMatch(.*)*', redirect: '/dashboard'}
];

I searched and found that router-link-active class should be automatically applied to Program link when the route starts with /programs, but it is not working in my case.

EDIT

I have found a workaround by manually matching route and appending active class to the router link. But I still expect an easier way to do this.

In my Program link:

<router-link to="/programs" :class="{'router-link-active': $route.fullPath.match(/\b\programs/) }">
        <span>Programs</span>
</router-link>

I am using regex to match path pattern. So, every route that contains programs will get active class here.

1

There are 1 answers

1
Barnum Taint On

dont u gotta do that in the component and not in the router?

take what i say with a grain of salt im