Vue implements three-level navigation display and hiding

Vue implements three-level navigation display and hiding

This article example shares the specific code of Vue to realize the display and hiding of three-level navigation for your reference. The specific content is as follows

Requirement description:

To realize the display and hiding of the three-level navigation of the sidebar. Click on one of the primary navigations to display the secondary navigation of that primary navigation, then click to close that secondary navigation. For other items in the primary navigation, expand the secondary navigation. Closes the secondary navigation of other primary navigations.

The effect is as follows:

Code:

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <HelloWorld msg="Welcome to Your Vue.js App" />
    <div class="first" v-for="(item, key) in navLists" :key="key">
      <li>
        <span @click="handleClick(key)"> {{ item.title }}</span>
        <div
          v-for="(item2, key2) in item.child"
          :key="key2"
          class="second"
          v-show="show2 && currIndex == key"
        >
          <p @click="secondClick(key2)">{{ item2.subTitle }}</p>
          <div
            v-for="(item3, key3) in item2.threeChild"
            :key="key3"
            class="three"
           v-show="show3 && currIndex2 == key2"
          >
            {{ item3.threeTitle }}
          </div>
        </div>
      </li>
    </div>
  </div>
</template>
 
<script>
import HelloWorld from "./components/HelloWorld.vue";
 
export default {
  name: "App",
  components:
    HelloWorld,
  },
  data() {
    return {
      i: 0,
 
      show3: false,
      show2: false,
      navLists: [
        {
          title: "Project Information",
          child: [
            {
              subTitle: "Project Introduction",
              esubTitle: "#projectIntroduction",
              threeChild: [
                { threeTitle: "Three-level navigation" },
                { threeTitle: "Three-level navigation" },
                { threeTitle: "Three-level navigation" },
              ],
            },
            {
              subTitle: "Sample Information",
              threeChild: [
                { threeTitle: "Third level navigation 22" },
                { threeTitle: "Third level navigation 22" },
                { threeTitle: "Third level navigation 22" },
              ],
            },
 
            {
              subTitle: "Sample Information",
              threeChild: [
                { threeTitle: "Third level navigation 33" },
                { threeTitle: "Third level navigation 33" },
                { threeTitle: "Third level navigation 33" },
              ],
            },
          ],
        },
        {
          title: "Project Information 2",
          child: [
            {
              subTitle: "Project Introduction 22",
              threeChild: [
                { threeTitle: "Three-level navigation 44" },
                { threeTitle: "Level 3 Guide 44" },
                { threeTitle: "Third level navigation 22" },
              ],
            },
            {
              subTitle: "Sample Information 22",
            },
          ],
        },
        {
          title: "Project Information 3",
          eTitle: "#projectMessage",
          child: [
            {
              subTitle: "Project Introduction 33",
              esubTitle: "#projectIntroduction",
            },
            {
              subTitle: "Sample Information 33",
              esubTitle: "#sampleInformation",
            },
          ],
        },
        {
          title: "Project Information 2",
          child: [
            {
              subTitle: "Project Introduction 22",
            },
            {
              subTitle: "Sample Information 22",
            },
          ],
        },
        {
          title: "Project Information 3",
          child: [
            {
              subTitle: "Project Introduction 33",
            },
            {
              subTitle: "Sample Information 33",
            },
          ],
        },
      ],
 
      currIndex: "", //Current index spanIndex: [], //Index array arrIndex: "", //Used to determine whether to find the current index by indexing the array. -1 means not found, 0 means found.
 
      currIndex2: "", //Current index of secondary navigation spanIndex2: [], //Index array arrIndex2: "", //Used to determine whether to find the current index by indexing the array. -1 means not found, 0 means found.
    };
  },
  methods: {
    handleClick(index) {
      // Initialize the third-level navigation, which is not displayed by default.
      this.show3 =false;
      this.spanIndex2.splice(-1, 1);
 
      // Current index = index
      this.currIndex = index;
      console.log("current index", index);
      // Determine whether the current index is in the index array spanIndex. There are only two possible values ​​for arrIndex: -1 not found. 0 found.
      this.arrIndex = this.spanIndex.indexOf(index);
      console.log("arrIndex", this.arrIndex);
 
      if (this.arrIndex == 0) {
        //arrIndex == 0, the index is found, delete the index in the index array spanIndex, and hide the secondary navigation.
        this.spanIndex.splice(this.arrIndex, 1);
        this.show2 = false;
      } else {
        // arrIndex ==-1, not found, splice(-1,1) deletes 1 value from the end of the spanIndex array and adds the current index to the index array spanIndex, show2 is true, showing the secondary navigation,
        this.spanIndex.splice(this.arrIndex, 1);
        this.spanIndex.push(index);
        this.show2 = true;
      }
      
      console.log("index array", this.spanIndex);
    },
 
    secondClick(index) {
      console.log(index);
      // Current index = index
      this.currIndex2 = index;
      // Determine whether the current index is in the index array spanIndex. There are only two possible values ​​for arrIndex: -1 not found. 0 found.
      this.arrIndex2 = this.spanIndex2.indexOf(index);
      console.log("arrIndex2", this.arrIndex2);
 
      if (this.arrIndex2 == 0) {
        //arrIndex == 0, the index is found, delete the index in the index array spanIndex, and hide the secondary navigation.
        this.spanIndex2.splice(this.arrIndex2, 1);
        this.show3 = false;
      } else {
        // arrIndex ==-1, not found, splice(-1,1) deletes 1 value from the end of the spanIndex array and adds the current index to the index array spanIndex, show2 is true, showing the secondary navigation,
        this.spanIndex2.splice(this.arrIndex2, 1);
        this.spanIndex2.push(index);
        this.show3 = true;
      }
       console.log("index array 2", this.spanIndex2);
    },
  },
};
</script>
 
<style>
p {
  padding: 5px 0;
  margin-block-start: 0;
  margin-block-end: 0;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.first {
  width: 200px;
  font-size: 24px;
  font-weight: bold;
  /* height: 60px; */
  /* background:red; */
}
.first:hover {
  cursor: pointer;
 
  /* color:red; */
}
.second {
  font-size: 18px;
  font-weight: normal;
  background: #eee;
  margin-left: 50px;
}
.three {
  background: yellow;
  margin-left: 20px;
  font-size: 14px;
}
</style>

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:
  • Implement the function of hiding the navigation bar on some routing pages in Vue
  • Vue routing meta set navigation hide and show function sample code
  • Analysis of Vue's method of dynamically displaying and hiding bottom navigation

<<:  When Navicat connects to MySQL, it reports 10060, 1045 errors and the location of my.ini

>>:  Python Flask WeChat applet login process and login api implementation code

Recommend

11 Reasons Why Bootstrap Is So Popular

Preface Bootstrap, the most popular front-end dev...

How to deploy tomcat in batches with ansible

1.1 Building the Directory Structure This operati...

Detailed explanation of Docker Swarm service orchestration commands

1. Introduction Docker has an orchestration tool ...

How to migrate mysql storage location to a new disk

1. Prepare a new disk and format it with the same...

Analysis of the difference between HTML relative path and absolute path

HTML beginners often encounter the problem of how ...

How to deploy Redis 6.x cluster through Docker

System environment: Redis version: 6.0.8 Docker v...

Windows Server 2019 Install (Graphical Tutorial)

Windows Server 2019 is the latest server operatin...

A brief analysis of the function calling process under the ARM architecture

Table of contents 1. Background knowledge 1. Intr...

How to dynamically modify the replication filter in mysql

MySQL dynamically modify replication filters Let ...

Basic implementation method of cross-component binding using v-model in Vue

Hello everyone, today we will talk about how to u...

MySQL 8.0.12 installation steps and basic usage tutorial under Windows

This article shares the installation steps and us...

Two methods of MySql comma concatenation string query

The following two functions are used in the same ...

Problems and solutions encountered when connecting node to mysql database

I installed a new version of MySQL (8.0.21) today...

20 CSS coding tips to make you more efficient (sorted)

In this article, we would like to share with you ...