Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

router-link with vue and vuetify confusion

How to use vue-router with using that predefined template:

https://vuetifyjs.com/examples/layouts/google-contacts

I have added a link in my items object

 items: 
[{ icon: 'dashboard' text: 'Home', link: '/'},

{ icon: 'dashboard' text: 'Account', link: '/account'},

I am confused where to put the router-link component.

like image 911
TheBAST Avatar asked Dec 01 '17 03:12

TheBAST


People also ask

How does Vue router-link work?

A router-link component renders an a tag by default (you can change that). Every time the route changes, either by clicking a link or by changing the URL, a router-link-active class is added to the element that refers to the active route, allowing you to style it.

How do I redirect my Vue router?

But what if you want to redirect programmatically, without using an anchor tag at all? If you're using Vue Router you'll push the new URL onto the router in order to do a redirect: this. $router. push('www.yoursite.com/blog') .

How do I add a router to my vuetify app?

We’ll combine the two, inserting the router view and router links into vuetify’s layout. The router view will live inside the <v-content> which is the main app content, and the router links will stay in the <v-toolbar>. That’s it, check your localhost to see the router buttons in action:

What is vue router in Vue?

In Vue.js, you can create several views using the first-party library Vue Router. This router makes an association with a view to a URL. In this tutorial, you are going to learn how to add the Vue Router library, integrate it into your project, and create dynamically generated routes.

How to use router-link component in Vue?

The router-link component creates an <a> tag that's configured to work correctly with Vue router. For example, given the below Vue code: Vue will render the below HTML. Note that <router-link> becomes a plain-old <a> tag.

How to get params from the router in vue router?

We are using the ‘meta’ param from Vue Router and inside that creating an attribute called ‘breadCrumb’ which returns an array of objects. But when we need to receive params from the router, we can define a function to return it. This array will be injected into the v-breadcrumb component.


3 Answers

v-list-tile, v-btn, and v-card all extend router-link, so you can use any of the router-link attributes directly on those components instead.

In your case you can just use <v-list-tile :to="item.link">

like image 98
Kael Watts-Deuchar Avatar answered Oct 29 '22 22:10

Kael Watts-Deuchar


I had the same problem, and I solved it like this:

<v-list-item v-else :key="item.text" link>
<!-- to -->
<v-list-item v-else :key="item.text" :to="item.link" link>


<v-list-item v-for="(child, i) in item.children" :key="i" link>
<!-- to -->
<v-list-item v-for="(child, i) in item.children" :key="i" :to="child.link" link>


JS

{ icon: "mdi-history", text: "Recientes", link: "/" },

Don't forget to put <router-view /> in the container.

    <v-content>
      <v-container class="fill-height" fluid>
        <router-view />
      </v-container>
    </v-content>
like image 45
Jean Paul Beard Avatar answered Oct 29 '22 22:10

Jean Paul Beard


An example:

<template>
  <v-navigation-drawer
    app
    clipped
    permanent
    expand-on-hover
  >
    <v-list>
      <v-list-item class="px-2">
        <v-list-item-avatar>
          <v-img src="https://randomuser.me/api/portraits/women/85.jpg"></v-img>
        </v-list-item-avatar>
      </v-list-item>

      <v-list-item link>
        <v-list-item-content>
          <v-list-item-title class="title">Sandra Adams</v-list-item-title>
          <v-list-item-subtitle>[email protected]</v-list-item-subtitle>
        </v-list-item-content>
      </v-list-item>
    </v-list>

    <v-divider></v-divider>

    <v-list
      nav
      dense
    >
      <v-list-item v-for="(item, i) in items" :key="i" :to="item.link" link>
        <v-list-item-icon>
          <v-icon>{{item.icon}}</v-icon>
        </v-list-item-icon>
        <v-list-item-title>{{item.text}}</v-list-item-title>
      </v-list-item>
    </v-list>
  </v-navigation-drawer>
</template>

<script>
export default {
  name: 'MenuDrawer',

  data: () => ({
    items: [
      {
        icon: 'mdi-folder',
        text: 'Home',
        link: '/',
      },
      {
        icon: 'mdi-account-multiple',
        text: 'Starred',
        link: 'https://github.com/vuetifyjs/vuetify',
      },
      {
        icon: 'mdi-star',
        text: 'About',
        link: '/about',
      },
    ],
  }),
};
</script>
like image 36
Jhonny Ramirez Zeballos Avatar answered Oct 29 '22 22:10

Jhonny Ramirez Zeballos