How can I dynamically add an active
class to the <a>
element of a sibling <router-link>
when clicking on the sub menu
in the provided template?
<ul class="depth1">
<li class="m1">
<router-link to="/introduce/Introduce" @click="selected = 1" :class="{ active: selected == 1 }"><span>menu1</span></router-link>
<ul class="depth2 sm1">
<li><router-link to="" @click="selected = 1">sub menu</router-link></li>
<li><router-link to="" @click="selected = 1">sub menu</router-link></li>
<li><router-link to="" @click="selected = 1">sub menu</router-link></li>
</ul>
</li>
<li class="m2">
<router-link to="/introduce/Introduce" @click="selected = 2" :class="{ active: selected == 2 }"><span>menu2</span></router-link>
<ul class="depth2 sm1">
<li><router-link to="" @click="selected = 2">sub menu</router-link></li>
<li><router-link to="" @click="selected = 2">sub menu</router-link></li>
<li><router-link to="" @click="selected = 2">sub menu</router-link></li>
</ul>
</li>
</ul>
<script>
export default {
data: function () {
return {
selected: false,
};
},
methods: {
},
};
</script>