Vue recursively implements custom tree components

Vue recursively implements custom tree components

This article shares the specific code of Vue recursively implementing a custom tree component for your reference. The specific content is as follows

1. In tree/index.vue:

<template>
  <div>
      <ul>
          <item :model='data'></item>
      </ul>
  </div>
</template>
 
<script>
import Item from './item'
export default {
    components:{
        Item
    },
    data(){
        return {
            data:{
                title:"Level 1",
                children:[
                    {
                        title:"Level 1-1",
                        children:[
                            {
                                title:"Level 3 1-1-1",
                                children:[
                                    {
                                        Title:"Level 4 1-1-1-1",
                                        children:[
                                            {
                                                title:"Level 5 1-1-1-1-1"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    },{
                        title:'Level 1-2',
                        children:[
                            {
                                Title:"Level 3 1-2-1"
                            }
                        ]
                    }
                ]
            }
        }
    }
}
</script>

2. item.vue component:

<template>
  <li>
      <div @click="toggle">
          {{model.title}}
          <span v-if="isFolder">[{{open?'-':'+'}}]</span>
      </div>
      <ul v-show="open" v-if="isFolder">
          <item v-for="(child,index) in model.children" :model='child' :key='index'></item>
      </ul>
  </li>
</template>
 
<script>
export default {
    name:'Item',
    props:{
        model:{
            type:Object,
            required:true
        }
    },
    data(){
        return {
            open:false
        }
    },
    computed:{
        isFolder(){
           return this.model.children && this.model.children.length>0
        }
    },
    methods:{
        toggle(){
            if (this.isFolder) this.open =!this.open
        }
    }
}
</script>

3. Use in any component:

<template>
  <div class="index">
      <Tree></Tree>
  </div>
</template>
 
<script>
    import Tree from "@/components/tree"
    components:{
        Tree
    }
</script>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to implement Vue's tree component
  • Use vant TreeSelect classification selection component operation in vue
  • Vue uses zTree plug-in to encapsulate tree component operation example
  • Vue recursive component + Vuex development tree component Tree--simple implementation of recursive component
  • Vue component tree implements tree menu
  • Vue2 component tree implements unlimited level tree menu
  • Vue.js component tree realizes multi-level linkage between provinces and cities
  • Vue.js component tree implements unlimited level tree menu

<<:  Detailed explanation of several ways to obtain the PID (TID, LWP) of Linux threads

>>:  Detailed explanation of three relationship examples of MySQL foreign keys

Recommend

JavaScript to achieve the effect of tab bar switching

Tab bar: Click different tabs to display differen...

Example analysis of mysql shared lock and exclusive lock usage

This article uses examples to illustrate the usag...

How to make your JavaScript functions more elegant

Table of contents Object parameters using destruc...

How to change the encoding of MySQL database to utf8mb4

The utf8mb4 encoding is a superset of the utf8 en...

Generate OpenSSL certificates in Linux environment

1. Environment: CentOS7, Openssl1.1.1k. 2. Concep...

Detailed explanation of Nginx forwarding socket port configuration

Common scenarios for Nginx forwarding socket port...

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...

HTTP Status Codes

This status code provides information about the s...

The difference between MySQL count(1), count(*), and count(field)

Table of contents 1. First look at COUNT 2. The d...

Some tips on using the HTML title attribute correctly

If you want to hide content from users of phones, ...

MySQL 5.7.27 installation and configuration method graphic tutorial

MySQL 5.7.27 detailed download, installation and ...

Let's talk about the v-on parameter problem in Vue

Use of v-on:clock in Vue I'm currently learni...

How to use Vue's idea to encapsulate a Storage

Table of contents background Function Purpose Ide...

Common pitfalls of using React Hooks

React Hooks is a new feature introduced in React ...