亲宝软件园·资讯

展开

vue三级菜单 vue递归实现三级菜单

进击的_菜鸡 人气:8
想了解vue递归实现三级菜单的相关内容吗,进击的_菜鸡在本文为您仔细讲解vue三级菜单的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:vue,递归,三级菜单,下面大家一起来学习吧。

父组件

<template>
    <div class="menu-level-menu menu-level-menu-enter"  v-if="showLevelMenu">
      <menu-item class="menu-item" :menuDate="menuList"></menu-item>
    </div>
</template>

子组件

<template>
  <div>
    <div class="" v-for="(menu, index) in menuDate" :key="index">
    // 每一个菜单项
      <div class="menu-row" @click="menuSpread(menu)"
           :class="[{'menu-row-selected': menu.selected && menu.children.length <= 0}]">
        <div class="menu-row-left">
          <div class="menu-row-left-line" :class="[{'menu-selected': menu.selected && menu.children.length <= 0}]"></div>
          <i class="iconfont" :class="[menu.menuIcon, {'color-icon': showIconColor(menu)}]"></i>
        </div>
        <div class="menu-row-right">
          <span :class="[{'font-16': menu.level === '0'}]">{{menu.menuName}}</span>
          <i class="c" v-if="menu.children.length <= 0"></i>
          <i class="iconfont icon-liebiaoxiala" v-if="menu.children.length>0 && !menu.selected"></i>
          <i class="iconfont icon-liebiaoshouqi" v-if="menu.children.length>0 && menu.selected"></i>
        </div>
      </div>
      // 递归展示菜单
      <menu-item v-show="menu.selected" v-if="menu.children.length>0" :menuDate="menu.children"></menu-item>
    </div>
  </div>
</template>
<script>
  export default {
    props: {
      menuDate: Array
    },
    name: 'MenuItem',
    methods: {
      menuSpread (menu) {
        if (menu.menuRouter) this.$router.push(menu.menuRouter);
        menu.selected = !menu.selected;
        this.recursion(this.menuDate, menu);
      },
      recursion (all, temp) {
        all.forEach(item => {
          if (item.menuName !== temp.menuName) {
            item.selected = false;
            this.recursion(item.children, temp);
          }
        });
      },
      showIconColor (menu) {
        let show = false;
        if (menu.level === '0') {
          menu.children.forEach(item => {
            if (item.children.length <= 0 && item.selected) {
              show = true;
            }
            if (item.children.length > 0) {
              item.children.forEach(item => {
                if (item.selected) {
                  show = true;
                }
              });
            }
          });
        }
        return show;
      }
    }
  };
</script>

效果图

加载全部内容

相关教程
猜你喜欢
用户评论