Repository navigation
Navigation Menu Link Item - Setting Active Dynamically #7410
|
Hi, I came across an issue and an AI found me the answer. Solution is not mentioned in the Official Documentation. Solution by Gemini AI - const links = [ { Similar Discussion - #6802 I would be happy to update the docs. Someone will have to guide me on the first commit. Thanks! |
Replies: 1 comment 1 reply
|
Here is the idiomatic way to handle this in Nuxt UI, along with a step-by-step guide on how you can contribute this to the documentation. 1. The Idiomatic SolutionInstead of hardcoding every child route in a manual equality check, use Because <script setup lang="ts">
const route = useRoute()
const items = computed(() => [
{
label: 'Businesses',
icon: 'i-lucide:flame',
to: '/businesses/list',
// Automatically active for '/businesses/list', '/businesses/123', etc.
active: route.path.startsWith('/businesses'),
},
{
label: 'Dashboard',
icon: 'i-lucide:layout-dashboard',
to: '/dashboard',
active: route.path === '/dashboard',
}
])
</script>
<template>
<UNavigationMenu :items="items" />
</template>Why this works best:
2. How to Contribute this to the Nuxt UI DocsContributing to
|
Hi @dragonsdenexotech
Here is the idiomatic way to handle this in Nuxt UI, along with a step-by-step guide on how you can contribute this to the documentation.
1. The Idiomatic Solution
Instead of hardcoding every child route in a manual equality check, use
computed()withroute.path.startsWith().Because
links/itemsis an array evaluated during component setup, it must be wrapped inside acomputed()property so Vue's reactivity system recalculates theactivestatus whenever the user navigates between routes: