I am currently working on customizing the navigation for my Vue application. My goal is to create a main menu at the top that displays all the root level routes, and a side menu that appears when navigating through child routes. Here is an example of what I'm trying to achieve:
https://i.sstatic.net/C0VFJ.png
The current design features the main navigation with routing links at the top and the router-view below it. I have successfully implemented the side menu to only display when selecting "Travellers" and it updates the components/content correctly. However, I am facing issues with routing. When clicking on a link in the sub-menu, it does not append to the current path. For example, when I click on "View" while on localhost/Traveler, the URL changes to localhost/View/ instead of localhost/Traveler/View. Additionally, the selection in the top menu gets unselected when choosing something in the child menu.
https://i.sstatic.net/zb0bx.png
I also encounter difficulty accessing pages via paths like localhost/Traveler/View; only localhost/View seems to work.
As I started looking into the documentation on nested routes during this post, I realized that I may need to create a new router at each level, which I haven't done in my current code below.
Furthermore, I am unsure how to access the children of the current route. I attempted to display them using the following code:
<h2>Route: {{ $route.name }}</h2>
<ul id="example-1">
<li v-for="child in $route.children">
{{ child.name }}
</li>
</ul>
However, I did not get any results. Should the children be passed as parameters or is there another method to access them easily?
Any guidance or assistance on this matter would be greatly appreciated.
Root
This section contains the top Menu-Nav with router links and the router-view component below it.
<template>
<div id="app" class="container-fluid">
<div class="row">
<div style="width:100%">
<nav-menu params="route: route"></nav-menu>
</div>
</div>
<div class="row">
<div>
<router-view></router-view>
</div>
</div>
</div>
</template>
<script>
import NavMenu from './nav-menu'
export default {
components: {
'nav-menu': NavMenu
},
data() {
return {}
}
}
</script>
Top Nav-Menu
This menu is populated with the available routes.
<template>
<nav class="site-header sticky-top py-1">
<div class="container d-flex flex-column flex-md-row justify-content-between">
<a class="nav-item" v-for="(route, index) in routes" :key="index">
<router-link :to="{path: route.path, params: { idk: 1 }}" exact-active-class="active">
<icon :icon="route.icon" class="mr-2" /><span>{{ route.display }}</span>
</router-link>
</a>
</div>
</nav>
</template>
<script>
import { routes } from '../router/routes'
export default {
data() {
return {
routes,
collapsed: true
}
},
methods: {
toggleCollapsed: function (event) {
this.collapsed = !this.collapsed
}
}
}
</script>
Traveler Page/View
This page represents the Traveller Page, featuring a sidebar menu and another router view for content:
<template>
<div id="app" class="container-fluid">
<div class="wrapper">
<traveler-menu params="route: route"></traveler-menu>
<div id="content">
<router-view name="travlerview"></router-view>
</div>
</div>
</div>
</template>
<script>
import TravelerMenu from './traveler-menu'
export default {
components: {
'traveler-menu': TravelerMenu
},
data() {
return {}
}
}
</script>
Side Bar/ Traveler Menu
<template>
<nav id="sidebar">
<div class="sidebar-header">
<h3>Route's Children:</h3>
</div>
<ul class="list-unstyled components">
<li>
<a class="nav-item" v-for="(route, index) in travelerroutes" :key="index">
<router-link :to="{path: route.path, params: { idk: 1 }}" exact-active-class="active">
<icon :icon="route.icon" class="mr-2" /><span>{{ route.display }}</span>
</router-link>
</a>
</li>
</ul>
</nav>
</template>
<script>
import { travelerroutes } from '../../router/travelerroutes'
export default {
data() {
console.log(travelerroutes);
return {
travelerroutes,
collapsed: true
}
},
methods: {
toggleCollapsed: function (event) {
this.collapsed = !this.collapsed
}
}
}
</script>
Routes
import CounterExample from 'components/counter-example'
import FetchData from 'components/fetch-data'
import HomePage from 'components/home-page'
import TestPage from 'components/test-page'
import Travelers from 'components/Traveler/traveler-root'
import { travelerroutes } from './travelerroutes'
export const routes = [
{ name: 'home', path: '/', component: HomePage, display: 'Home', icon: 'home' },
{ name: 'counter', path: '/counter', component: CounterExample, display: 'Counter', icon: 'graduation-cap' },
{ name: 'fetch-data', path: '/fetch-data', component: FetchData, display: 'Fetch data', icon: 'list' },
{ name: 'test-page', path: '/test-page', component: TestPage, display: 'Test Page', icon: 'list' },
{
name: 'traveler-root', path: '/traveler', component: Travelers, display: 'Travelers', icon: 'list', children: travelerroutes
}
]
Traveler Routes (travelerroutes.js)
import TestPage from 'components/test-page'
import ViewTravelers from 'components/Traveler/TravelerPages/view-travelers'
export const travelerroutes = [{
name: 'View',
path: '/View',
display: 'View', icon: 'list',
components: {
travlerview: TestPage
}
},
{
name: 'Create',
path: '/Create',
display: 'Create', icon: 'list',
components: {
travlerview: ViewTravelers
}
},
{
name: 'Edit',
path: '/Edit',
display: 'Edit', icon: 'list',
components: {
travlerview: ViewTravelers
}
}];
router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import { routes } from './routes'
Vue.use(VueRouter)
let router = new VueRouter({
mode: 'history',
routes
})
export default router
app.js
import Vue from 'vue'
import axios from 'axios'
import router from './router/index'
import store from './store'
import { sync } from 'vuex-router-sync'
import App from 'components/app-root'
import { FontAwesomeIcon } from './icons'
// Register global components
Vue.component('icon', FontAwesomeIcon)
Vue.prototype.$http = axios
sync(store, router)
const app = new Vue({
store,
router,
...App
})
export {
app,
router,
store
}
Please let me know if you require additional information, context, or code snippets.