Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS Router - How do I stop multiple active routes when using sub routes and Vuetify?

I am struggling to stop the default '' Home.vue route not be active in Vuetify i.e. when I click uploads or settings the default Home.vue is still active. See here:

enter image description here

I assume it's something to do with it being a sub route of /webapp but I'm banging my head against a wall..

This is the important bit in my routes:

    {
  path: '/webapp',
  name: 'webapp',
  component: () => import('./views/Webapp.vue'),
  children: [
          {
            path: '',
            component: () => import('./components/Home.vue'),
          },
          {
            path: 'uploads',
            component: () => import('./components/Uploads.vue'),
          },
          {
            path: 'settings',
            component: () => import('./components/Settings.vue'),
          }
  ]
}

This is my Vuetify drawer:

<v-navigation-drawer
  v-model="drawer"
  app

>
  <v-list dense>

    <v-list-item
      v-for="item in items"
      :key="item.title"
      :to="item.route"

    >
      <v-list-item-icon>
        <v-icon>{{ item.icon }}</v-icon>
      </v-list-item-icon>

      <v-list-item-content>
        <v-list-item-title>{{ item.title }}</v-list-item-title>
      </v-list-item-content>
    </v-list-item>

  </v-list>
</v-navigation-drawer>
like image 779
Johnny John Boy Avatar asked Aug 07 '19 12:08

Johnny John Boy


People also ask

Can we define multiple router outlets in a component Vue?

Instead of having one single outlet in your view, you can have multiple and give each of them a name. A router-view without a name will be given default as its name. A working demo of this example can be found here.

How do I create a dynamic route Vue?

Adding dynamic routes in VueUpdate the router/index. js file with the new route in the routes array. Remember to include the import for the Post component. Restart your app and head to localhost:8080/post/dynamic-routing in your browser.

What is hash mode in VUE router?

The default mode for vue-router is hash mode - it uses the URL hash to simulate a full URL so that the page won't be reloaded when the URL changes. To get rid of the hash, we can use the router's history mode, which leverages the history.


1 Answers

You have to add exact (or exact={true}) prop to <v-list-item> component:

<v-list-item
  v-for="item in items"
  :key="item.title"
  :to="item.route"
  exact

></v-list-item>

On v-list-item docs for exact:

Exactly match the link. Without this, '/' will match every route. You can find more information about the exact prop on the vue-router documentation.

You can check that out:

https://vuetifyjs.com/en/components/list-item-groups#list-item-groups

like image 52
Nelio Avatar answered Oct 21 '22 14:10

Nelio