I am having trouble adding an active class when clicking a link.
I have the dynamic class setup with a click listener that passes an argument to the function but the class does not update. I am not sure why. The 'Dashboard' link is red when page is refreshed so I know it is working to an extent.
<template>
<b-list-group>
<b-list-group-item
v-for="(link, index) in menu"
:key="index"
:href="link.sectionId"
:class="{active: link.title === selectedLink}"
:click="isActive(link.title)"
>
{{link.title}}
</b-list-group-item>
</b-list-group>
</template>
<script>
export default {
data() {
return {
selectedLink: 'Dashboard',
menu: [
{
title: 'Dashboard',
icon: labStuffs,
sectionId: '#'
},
{
title: 'Lactose intolerance',
icon: labStuffs,
sectionId: '#'
}
]
}
},
methods: {
isActive(title) {
this.selectedLink === title
}
}
}
</script>
<style scoped lang="scss">
.active {
background-color: red;
}
</style>
Expecting the background colour of clicked link to change. Only the dashboard link is red and whenever I click anything else, nothing happens.
this.selectedLink === titlein your method bethis.selectedLink = titleinstead?b-list-group-itemwhich renders as an anchor tag in the html. Might want to handle route change and active class in the same logic? Also, shoudn't it be@clickinstead of:click