Vue-router: Loop over routes to dynamically create <router-link>
Asked Answered
N

1

13

I would like to loop over my routes using v-for in order to generate the <router-link>s dynamically in the template section.

Currently I have no idea how to address the router param, that is defined in main.js, in the template section of my jvds-navigation.vuecomponent.

Here are the scripts, I added comments, where my problem occurs:

main.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'

// Import the routes file
import { routes } from './router/routes.js'

// Router
Vue.use(VueRouter)
const router = new VueRouter({
    routes,
    base: __dirname,
    mode: 'history'
})

new Vue({
    router, // short for "router: router"
    render: h => h(App),
}).$mount('#app')

App.vue

<template>
    <span id="app" class="o-root">
        <jvds-navigation class="..."></jvds-navigation>
        <router-view></router-view>
    </span>
</template>

<script>
import JvdsNavigation from "@/components/layout/jvds-navigation.vue"

export default {
name: "App",
    components: {
        JvdsNavigation
    }
};
</script>

jvds-navigation.vue

<template>
<div>
    <div class="[...]">

    <!-- 
        `nav` should have a `v-for` over the routes imported 
        in main.js (example given, but wrong). 
        But how can I address routes here?
    -->

    <nav class="[...]" v-for="routeItem in routes">
        <!-- These should be created dynamically... -->
        <router-link to="/about">
            <a>About</a>
        </router-link>
        <router-link to="/cv">
            <a>CV</a>
        </router-link>
        <router-link to="/contact">
            <a>Contact</a>
        </router-link>
  </nav>
  </div>
</div>

<script>
// How do I get `routes` (imported in main.js) recognized by
// this component in order to be able to loop over it in the
// `template` section?
export default {
  name: "jvds-navigation"
};
</script>

Thank you for any tips, tricks and hints!

Neurovascular answered 28/7, 2018 at 9:14 Comment(0)
N
22

Use v-for on router-link. I've used this.$router.options.routes to get all routes. You can also pass it as prop to jvds-navigation.

This is a small example of you how can do it:

example routes

const routes = [
  {
    path: '/',
    component: Home,
    name: 'Home',
  },
  {
    path: '/about',
    component: About,
    name: 'About'
  },
];

template

<nav>
  <router-link v-for="route in routes" :key="route.path" :to="route.path">
    {{route.name}}
  </router-link>
</nav>

script

export default {
  name: 'Navigation',
  data() {
    return {
      routes: this.$router.options.routes
    };
  }
  // or use computed
  // computed: {
  //   routes() {
  //     return this.$router.options.routes;
  //   }
  // }
}
Naughton answered 28/7, 2018 at 9:37 Comment(5)
is this deprecated ? "Cannot use v-for on stateful component root element because it renders multiple elements."Haven
It might be. I'll check it!Naughton
Not sure if I am breaking something, but I simply wrapped router-link in a seperate component and hand over the route as props. Then the loop is not on router-link but the surrounding component.Dreadful
I tried what you described and it worked for me. With the old router and the new one (> 4). You may have forgotten to set a name property in the routes object. Could you please create a jsfiddle or something similar? Maybe I can help you.Naughton
for those using <script setup> we cant use 'this' so instead const router = useRouter(); const myroutes = router.getRoutes();Metacarpal

© 2022 - 2024 — McMap. All rights reserved.