Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add links to Vuetify tree-view

Im using vuetify's treeview component and I want the last child in each node to be a router link, does anyone know how to achieve this?

Even if I could just bind an @click event that would work for me.

I cannot see in the docs or examples how to do this.

This is what my component looks like:

<template>
    <div>
       <v-treeview :items="items"></v-treeview>
    </div>
</template>

<script>
    export default {
        data: () => ({
            items: [
                {
                    name: 'Parent',
                    children: [
                        {
                            name: 'Child1',
                            to:'booking' // <---I want to put a router link here or click event handler
                        }
                    ]
                }
            ]
        })
    }
</script>
like image 627
Brad Avatar asked May 31 '26 17:05

Brad


1 Answers

You need to use a scoped slot for your labels:

<template>
    <div>
        <v-treeview :items="items">
            <template slot="label" slot-scope="props">
                <router-link :to="props.item.to">{{ props.item.name }}</router-link>
            </template>
        </v-treeview>
    </div>
</template>

You should also make sure to display data that do not have the to property without a link. You can use a simple v-if for that:

<template>
    <div>
        <v-treeview :items="items">
            <template slot="label" slot-scope="props">
                <router-link :to="props.item.to" v-if="props.item.to">{{ props.item.name }}</router-link>
                <span v-else>{{ props.item.name }}</span>
            </template>
        </v-treeview>
    </div>
</template>
like image 155
Hammerbot Avatar answered Jun 03 '26 06:06

Hammerbot