I’m having some trouble with vue-router. My routes are set up like this:
JavaScript
x
7
1
const routes = [
2
{ path: '/', component: a },
3
{ path: '/a', component: a },
4
{ path: '/b', component: b },
5
{ path: '/c', component: c }
6
]
7
As you can see, I want to load component a
when the page loads. But I don’t want the URL to change to /a
when the page loads. However I would like to trigger the router-link-active
-class on the <router-link>
related to the a
component, even though I’m still at /
.
I’ve tried with some simple JS like:
JavaScript
1
5
1
if(window.location.hash === '#/') {
2
var el = document.querySelector('#drawerList').firstElementChild;
3
el.className += 'router-link-active';
4
}
5
However vue is not removing the class again when I click on the link to /b
or /c
. Can any of you hint me in the corrent direction?
Advertisement
Answer
You can manually bind class to router-link like this
JavaScript
1
2
1
<router-link :class="{'router-link-active': $route.fullPath ==='/' || $route.fullPath === '/a'}" to="/a"></router-link>
2