Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

432
Views
Vue 3: Tree Menu Show

To be clear what I want to do... When Menu 1 is Clicked, its ul will be shown and likewise if Menu 1 is shown ul, and Menu 1 is Clicked, ul is not displayed. same applies to Menu 2

Can you show me as an example how can I do what I want to do?

Menu 1 Click Menu 1 ul show or ul show close

<ul>
    <li v-for="(item,index) in menuData" :key="index">
      <router-link :to="item.path" exact @click.stop="showMenu(index)">{{ item.name }}</router-link>
      <ul v-if="item.children" v-show="showCollapsed">
        <li v-for="(childItem,ChildIndex) in item.children" :key="ChildIndex">
          <router-link :to="childItem.path">{{ childItem.name }}</router-link>
        </li>
      </ul>
    </li>
  </ul>
<script>
export default {
  name: "App",
  data() {
    return {
      openshow: false,
      selected: null,
      menuData: [{name: "Home", path: "/",}, {name: "About", path: "/about",}, {name: "Menu 1", path: "#", children: [{name: "Menu 1.1", path: "/menu/1/1",}, {name: "Menu 1.2", path: "/menu/1/2",}, {name: "Menu 1.3", path: "/menu/1/3",},],}, {name: "Menu 2", path: "#", children: [{name: "Menu 2.1", path: "/menu/2/1",}, {name: "Menu 2.2", path: "/menu/2/2",}, {name: "Menu 2.3", path: "/menu/2/3",},],}, {name: "Gallery", path: "/gallery",}, {name: "Contact", path: "/contact",},],
    }
  },
  methods: {
    showMenu(index) {
      this.selected = index;
    },
    showCollapsed() {

    }
  }
}
</script>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Just use showMenu method with passed index and then check what ul to show passing index to showCollapsed :

const app = Vue.createApp({
   data() {
    return {
      openshow: false,
      selected: null,
      menuData: [{name: "Home", path: "/",}, {name: "About", path: "/about",}, {name: "Menu 1", path: "#", children: [{name: "Menu 1.1", path: "/menu/1/1",}, {name: "Menu 1.2", path: "/menu/1/2",}, {name: "Menu 1.3", path: "/menu/1/3",},],}, {name: "Menu 2", path: "#", children: [{name: "Menu 2.1", path: "/menu/2/1",}, {name: "Menu 2.2", path: "/menu/2/2",}, {name: "Menu 2.3", path: "/menu/2/3",},],}, {name: "Gallery", path: "/gallery",}, {name: "Contact", path: "/contact",},],
    }
  },
  methods: {
    showMenu(index) {
      this.selected === index ? this.selected = null : this.selected = index;
    },
    showCollapsed(idx) {
      return idx === this.selected || false
    }
  }
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <ul>
    <li v-for="(item,index) in menuData" :key="index" @click="showMenu(index)">
      <router-link :to="item.path" exact>{{ item.name }}</router-link>
      <ul v-if="item.children" v-show="showCollapsed(index)">
        <li v-for="(childItem,ChildIndex) in item.children" :key="ChildIndex">
          <router-link :to="childItem.path">{{ childItem.name }}</router-link>
        </li>
      </ul>
    </li>
  </ul>
</div>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!