简体   繁体   English

Vue3 树形菜单显示

[英]Vue3 Tree Menu Show

this is what i want to do with vue.js, If Menu 1 or Menu 2 is clicked among the items with submenu, its ul will be opened, and if Menu 1 is open, Menu 2 is clicked.这就是我想用 vue.js 做的,如果在带有子菜单的项目中单击Menu 1Menu 2 ,它的 ul 将被打开,如果Menu 1打开,则单击Menu 2 Close Menu 1 .关闭Menu 1 I couldn't do this, I would be happy if you help me.我做不到,如果你能帮助我,我会很高兴。 What I want to do is if one of the submenu is open, the 2nd submenu remains closed.我想要做的是,如果其中一个子菜单处于打开状态,则第二个子菜单保持关闭状态。 Codesandbox 代码沙盒

<template>
 <div class="navigation">
      <div class="container max-w-7xl mx-auto">
        <ul class="flex justify-end navbar-nav">
          <li v-for="(menu, index) in menuItem" :key="index">
            <a href="menu.href" @click.stop="showMenu()">{{ menu.title }}</a>
            <ul v-if="menu.children" :style="{ display: active ? 'block' : 'none' }" @click.stop>
              <li  v-for="(child, childIndex) in menu.children" :key="childIndex">
                <a href="child.href">{{ child.title }}</a>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
</template>
<script>
export default {
  name: "Home",
  data() {
    return {
      active: false,
      menuItem: [
        {
          title: "Home",
          href: "/home",
        },
        {
          title: "About",
          href: "/about",
        },
        {
          title: "Menu 1",
          href: "#",
          children: [
            {
              title: "Menu 1.1",
              href: "/menu/1/1",
            },
            {
              title: "Menu 1.2",
              href: "/menu/1/2",
            },
            {
              title: "Menu 1.3",
              href: "/menu/1/3",
            },
          ],
        },
        {
          title: "Menu 2",
          href: "#",
          children: [
            {
              title: "Menu 2.1",
              href: "/menu/2/1",
            },
            {
              title: "Menu 2.2",
              href: "/menu/2/2",
            },
            {
              title: "Menu 2.3",
              href: "/menu/2/3",
            },
          ],
        },
        {
          title: "Gallery",
          href: "/gallery",
        },
        {
          title: "Contact",
          href: "/contact",
        },
      ],
    };
  },
  methods: {
    showMenu() {
      this.active = !this.active;
    },
    close() {
      this.active = false;
    },
  },
  mounted() {
    document.addEventListener("click", this.close);
  },
  unmounted() {
    document.removeEventListener("click", this.close);
  },
};
</script>

Please take a look at following snippet (you need to use index):请查看以下代码片段(您需要使用索引):

 const app = Vue.createApp({ data() { return { menuItem: [{title: "Home", href: "/home",}, {title: "About", href: "/about",}, {title: "Menu 1", href: "#", children: [{title: "Menu 1.1", href: "/menu/1/1",}, {title: "Menu 1.2", href: "/menu/1/2",}, {title: "Menu 1.3", href: "/menu/1/3",},],}, {title: "Menu 2", href: "#", children: [{title: "Menu 2.1", href: "/menu/2/1",}, {title: "Menu 2.2", href: "/menu/2/2",}, {title: "Menu 2.3", href: "/menu/2/3",},],}, {title: "Gallery", href: "/gallery",}, {title: "Contact", href: "/contact",},], selected: null }; }, methods: { showMenu(i) { this.selected = i }, } }) app.mount('#demo')
 <script src="https://unpkg.com/vue@3.2.29/dist/vue.global.prod.js"></script> <div id="demo"> <div class="navigation"> <div class="container max-w-7xl mx-auto"> <ul class="flex justify-end navbar-nav"> <li v-for="(menu, index) in menuItem":key="index"> <a href="#" @click.stop="showMenu(index)">{{ menu.title }}</a> <ul:style="selected === index? 'display: block': 'display: none'"> <li v-for="(child, childIndex) in menu.children":key="childIndex"> <a href="#">{{ child.title }}</a> </li> </ul> </li> </ul> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM