I've been trying to add a .active class to the current link and I know it's been asked here before, but, none of the answers seems to match my need exactly.
It's a Vue component. I don't want to add vue-router. I want to add the active class dynamically to whichever link is clicked.
<template>
<nav>
<ul>
<li v-for="(item, $index) in items" :key="$index" >
<a :href="item.link">{{ item.name }}</a> //how do I dynamically add the active class here?
</li>
</ul>
</nav>
</template>
<script>
export default {
name: "Navbar",
props: {
items: {
type: Array,
default: null,
},
},
}
</script>
<style>
ul li .active {
background-color: red;
cursor: pointer;
}
</style>
The list of links:
const items = [
{
name: "Home",
link: "#home",
},
{
name: "About",
link: "#about"
},
{
name: "Contacts",
link: "#contact",
},
];