jrutter
jrutter

Reputation: 3213

Can I use Vuejs router routes array to dynamically create a nav?

Here is my code to setup the vuejs routes and pass into router:

export const routesArray = {
  routes: [
    {
      path: '/',
      name: 'Add Stash',
      component: Form
    },
    {
      path: '/log',
      name: 'Stash Log',
      component: Stitch
    },
    {
      path: '/user/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/user/login',
      name: 'Log in',
      component: Login
    }
  ]
}
export default new Router(routesArray)

I'm importing this code into a Vue Component and using it to loop through the array and dynamically output the navigation like this:

<ul class="nav">
  <li v-for="item in routes">
      <a :href="item.path"> {{item.name}}</a>
  </li>

This display works like a dream, but when I click on a nav item, it goes to a broken link: http://localhost:8080/log#/

Instead of this: http://localhost:8080/#/log

I'm not sure how to get around that hash? Any ideas?

Upvotes: 0

Views: 747

Answers (1)

CyberAP
CyberAP

Reputation: 1233

Use router-link instead of a.

<li v-for="item in routes">
    <router-link :to="item.path">{{item.name}}</router-link>
</li>

Upvotes: 1

Related Questions