I am currently utilizing this navbar. It works fine when the href links are unique, but I encounter an issue when two pages share the same link - the active class is applied to both list items with that link in the manner I have written.
Vue.component('navbar', {
template: `<svg xmlns="http://www.w3.org/2000/svg" width="15.352" height="15.355" viewBox="0 0 15.352 15.355">
<path id="Union_19" data-name="Union 19" d="M-19.5-15958.5l-7.5,7.5,7.5-7.5-7.5-7.5,7.5,7.5,7.5-7.5-7.5,7.5,7.5,7.5Z" transform="translate(27.176 15966.178)" fill="none" stroke="#bbb" stroke-width="0.5"/>
</svg>`
})
new Vue({
el: "#app",
data() {
return {
menu: [
{ to: { name: 'link1' }, name: 'page1' },
{ to: { name: 'sameLink' }, name: 'page2' },
{ to: { name: 'sameLink' }, name: 'page3' },
{ to: { name: 'link2' }, name: 'page4' }
]
}
}
});
.nav {
display: flex;
flex-wrap: nowrap;
margin-bottom: 0;
list-style: none;
font-size: .875rem;
overflow: auto;
background-color: $navBar-bg;
}
.navLink {
display: block;
color: $navBar-color;
text-decoration: none;
position: relative;
white-space: nowrap;
@include hover-focus() {
text-decoration: none;
color: $navBar-hover-color;
}
&.disabled {
color: $navBar-disabled-color;
pointer-events: none;
cursor: default;
}
:global(.show) > &,
:global(.active) > &,
.nav-link:global(.show),
.nav-link:global(.active) {
color: red;
&:after {
content: '';
display: block;
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: .1875rem;
background-color: currentColor;
}
}
}
<link type="text/css" rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cfef3f3e8efe8eefdecdca8b2a9b2af">[email protected]</a>/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bddfd2d2c9cec9cfdccd90cbc8d8fd8f938f8c938f">[email protected]</a>/dist/bootstrap-vue.css" />
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d4b48587d0f130b130c0f">[email protected]</a>/dist/vue.min.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e0828f8f949394928190cd969585a0d2ced2d1ced2">[email protected]</a>/dist/bootstrap-vue.min.js"></script>
<div id="app">
<ul class="nav">
<router-link
v-for="{ to, name } in menu"
v-slot="{ href: linkHref, navigate, isActive, isExactActive }"
:key="to.name || name"
:to="to"
>
<li v-else :class="['nav-item', isActive && 'active', isExactActive && 'exact-active']">
<a
v-t="name"
href="linkHref"
class="navLink"
/>
</li>
</router-link>
</ul>
</div>
Is there a way for me to use the same link for different pages, but only have the active class applied to the page that is currently being shown and truly active? These pages differ in content due to certain filters even though they share the same link.