I have a navbar at the bottom of my page which is in the App.vue component so that it sits on top of every page. What I would like to happen is that when a user navigates to a page, this page's button in the navbar highlights/changes class.
I am using composition API, but thought their may be a way of doing this in HTML without having to write in the script.
I can't seem to find a best practice online, please can someone help?. This was my attempt but it doesn't work. There must be a simpler way! Thank you.
HTML:
<ul class="navbar-nav flex-row justify-content-center">
<li class="nav-item px-2">
<router-link to="/" class="nav-link"><i class="fa-solid fa-house" :class="{'text-primary': currentRouteId === 1, 'text-muted': currentRouteId != 1}"></i></router-link>
</li>
<li class="nav-item px-2">
<router-link :to="{ name: 'Rooms' }" class="nav-link"><i class="fa-solid fa-square" :class="{'text-primary': currentRouteId === 2, 'text-muted': currentRouteId != 2}"></i></router-link>
</li>
<li class="nav-item px-2">
<router-link :to="{ name: 'Rooms' }" class="nav-link"><i class="fa-solid fa-video" :class="{'text-primary': currentRouteId ===3, 'text-muted': currentRouteId != 3}"></i></router-link>
</li>
<li class="nav-item px-2">
<router-link :to="{ name: 'Rooms' }" class="nav-link"><i class="fa-solid fa-shield" :class="{'text-primary': currentRouteId === 4, 'text-muted': currentRouteId != 4}"></i></router-link>
</li>
<li class="nav-item px-2">
<router-link :to="{ name: 'Rooms' }" class="nav-link"><i class="fa-solid fa-door-closed" :class="{'text-primary': currentRouteId === 5, 'text-muted': currentRouteId != 5}"></i></router-link>
</li>
</ul>
Script:
<script>
import { computed, ref, onUpdated } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
export default {
setup(){
const store = useStore()
const route = useRoute()
const pageParams = computed(() => store.state.pageParams)
const currentRouteName = computed(() => route.name)
var currentRouteId = ref(null)
//
onUpdated(() => {
if(currentRouteName.value === 'Home'){console.log('Home'); currentRouteId=1;}
if(currentRouteName.value === 'Rooms' || currentRouteName.value === 'Room'){console.log('Rooms'); currentRouteId=2;}
if(currentRouteName.value === 'CCTV'){console.log('CCTV'); currentRouteId=3;}
if(currentRouteName.value === 'Security'){console.log('Security'); currentRouteId=4;}
if(currentRouteName.value === 'Door Entry'){console.log('Door Entry'); currentRouteId=5;}
console.log(currentRouteId)
})
return { pageParams, currentRouteName, currentRouteId }
},
}
</script>