Method of implementing recursive components based on Vue technology

Method of implementing recursive components based on Vue technology

describe

This article introduces a method to implement recursive components based on Vue technology. It is very simple to use Vue to display the first-level list and the second-level list, but if you want to achieve infinite levels, just applying v-for one after another will not work. At this time, you need to use the recursive method. The so-called recursion is to continuously call itself. The recursive component is to continuously call its own component to achieve infinite list display. As shown below:

Code Implementation

1. Tree component

Create a tree.vue component in the directory.

<!-- tree tree component-->
<template>
  <div class="container">
      <div v-for="item in treeData" :key="item">
        <div class="row" @click="extend(item)">
            <span
                ref="icon"
                class="icon-common"
                :class="{
                    'icon-down': item.children,
                    'icon-radio': !item.children,
                    'icon-rotate': item.isExtend
                }"
            ></span>
            <span class="title">{{ item.title }}</span>
        </div>
        <div v-if="isExtend(item)" class="children">
            <tree :tree-data="item.children" :is-extend-all="isExtendAll" />
        </div>
  </div>
  </div>
</template>

<script>
export default {
    props: {
        // Component data treeData: {
            type: Array,
            default: [],
        },
        // Whether to expand all by default isExtendAll: {
            type: Boolean,
            default: true,
        }
    },
    methods: {
        // Expand the list extend(item) {
            if (item.children) {
                item.isExtend = !item.isExtend;
            }
        },
        isExtend(item) {
            const isExtend = !item.isExtend && true;
            return this.isExtendAll ? isExtend : !isExtend;
        }
    }
}
</script>

<style scoped>
.container {
    font-size: 14px;
}
.row {
    display: flex;
    align-items: center;
    cursor: pointer;
    margin-bottom: 10px;
}
/* ----------- Icon style START ------------- */
.icon-common {
    display: inline-block;
    transition: all .3s;
}
.icon-down {
    width: 0;
    height: 0;
    border: 4px solid transparent;
    border-top: 6px solid #000;
    border-bottom: none;
}
.icon-radio {
    width: 6px;
    height: 6px;
    background: #000;
    border-radius: 50%;
}
.icon-rotate {
    transform: rotate(-90deg);
}
/* ----------- Icon style END ------------- */
.title {
    margin-left: 10px;
}
.children {
    padding-left: 20px;
}
</style>

2. References

Import the tree component where you need it.

<template>
 <tree :tree-data="treeData" />
</template>

<script>
import Tree from './components/tree.vue';

export default {
 components:
  Tree,
 },
 data() {
  return {
   treeData: [
    {
     title: 'Level 1 List 1',
     children: [
      {
       title: 'Secondary List 1',
                            children: [
                                {
                                    title: 'Level 3 List 1',
                                }
                            ]
      },
      {
       title: 'Secondary List 2',
      }
     ]
    },
    {
     title: 'Level 1 List 2',
     children: [
      {
       title: 'Secondary List 1',
      },
      {
       title: 'Secondary List 2',
      }
     ]
    },
    {
     title: 'Level 1 List 3',
     children: [
      {
       title: 'Level 3 List 1',
      },
      {
       title: 'Level 3 List 2',
      },
      {
       title: 'Level 3 List 3',
      }
     ]
    }
   ]
  }
 }
}
</script>

Rendering

Summarize

This component only implements data display and some basic functions, which certainly does not meet the actual needs of some projects. If you want to use it, you need to expand and improve it on this basis. (For example, if you use this component to implement the left menu, you can configure the data yourself and slightly modify the component template to achieve click jump).

Component Function

  • Click to expand and collapse
  • Expand all

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:
  • Vue.js recursive component to implement tree menu (example sharing)
  • Vuejs uses recursive components to implement tree directories
  • Using Vue.js recursive components to implement a collapsible tree menu (demo)
  • Vue.js learning recursive components
  • Vue2 recursive component to implement tree menu
  • Vue.js recursive component realizes organizational structure tree and personnel selection function
  • Vue.js recursive component to build a tree menu
  • Vue recursive component + Vuex development tree component Tree--simple implementation of recursive component
  • Vue recursive component actual combat simple tree control example code
  • Vue uses recursive components to write example code for tree controls

<<:  Linux process management tool supervisor installation and configuration tutorial

>>:  MySQL 8.0.18 deployment and installation tutorial under Windows 7

Recommend

Detailed explanation of the process of using GPU in Docker

Table of contents Download tf-gpu Build your own ...

The complete usage of setup, ref, and reactive in Vue3 combination API

1. Getting started with setUp Briefly introduce t...

Why should you be careful with Nginx's add_header directive?

Preface As we all know, the nginx configuration f...

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

How to add abort function to promise in JS

Table of contents Overview Promise Race Method Re...

Implementing user registration function with js

This article example shares the specific code of ...

Detailed explanation of the installation steps of the MySQL decompressed version

1. Go to the official website: D:\mysql-5.7.21-wi...

A few front-end practice summaries of Alipay's new homepage

Of course, it also includes some personal experien...

Optimizing the slow query of MySQL aggregate statistics data

Written in front When we operate the database in ...

HTTP header information interpretation and analysis (detailed summary)

HTTP Header Explanation 1. Accept: Tells the web s...

A brief discussion on Axios's solution to remove duplicate requests

Table of contents 1. Cancel duplicate requests 2....

Related commands to completely uninstall nginx under ubuntu16.04

nginx Overview nginx is a free, open source, high...