vue2 elementUI递归渲染多级菜单栏

需要将组件拆分为两个:菜单栏容器一个,菜单栏列表一个

在这里插入图片描述

  1. 菜单栏列表:list.vue
    此处的fragment 是安装的插件:npm install vue-fragment --save,为了解决菜单栏折叠文字和>不隐藏。
<template>
  <fragment class="menu-list">
    <template v-for="item in data">
      <el-menu-item
        v-if="!item.children"
        :key="item.menuIdStr"
        :index="item.menuIdStr"
        @click="goPage(item)"
      >
        <i :class="item.icon"></i>
        <span slot="title">{{ item.menuName }}</span>
      </el-menu-item>
      <el-submenu v-else :key="item.menuIdStr" :index="item.menuIdStr">
        <template slot="title">
          <i :class="item.icon"></i>
          <span slot="title">{{ item.menuName }}</span>
        </template>
        <MenuList :data="item.children"></MenuList>
      </el-submenu>
    </template>
  </fragment>
</template>

<script>
/**
 * @author        全易
 * @time          2021-04-26 08:48:57  星期一
 * @description   菜单栏列表
 */
import Fragment from 'vue-fragment'
Vue.use(Fragment.Plugin)


export default {
  name: "MenuList",
  props: {
    data: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  methods: {
    // 跳转页面
    goPage(page) {
      // console.log(page);
      if (this.$route.path !== page.path) {
        this.$router.push(page.path);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.fa {
  vertical-align: middle;
  margin-right: 5px;
  width: 24px;
  text-align: center;
}
</style>
  1. 菜单栏容器:index.vue
<template>
  <el-menu
    class="box-card"
    unique-opened
    :collapse="$store.state.isCollapse"
    :default-active="$store.state.defaultPage"
    background-color="#2f3332"
    text-color="#fff"
    active-text-color="#ff9c40"
    :style="$store.state.width"
  >
    <el-menu-item index="Page-Home" @click="goPage({ url: '/home' })">
      <i class="fa fa-newspaper-o"></i>
      <span slot="title">看盘</span>
    </el-menu-item>
    <!-- <MenuList :data="$store.state.permissionMenu"></MenuList> -->
    <MenuList :data="menus"></MenuList>
  </el-menu>
</template>

<script>
/**
 * @author        全易
 * @time          2020-10-05 16:38:30  星期一
 * @description   菜单栏
 */
import MenuList from "./list";

export default {
  name: "Menu",
  components: {
    MenuList,
  },
  data() {
    return {
      menus: [
        {
          menuName: "设备管理",
          menuIdStr: "equipment/index",
          icon: "fa fa-tasks",
          path: "/equipment/index",
        },
        {
          menuName: "运行管理",
          menuIdStr: "running/index",
          icon: "fa fa-tasks",
          path: "/running/index",
        },
        {
          menuName: "档案管理",
          menuIdStr: "3",
          icon: "fa fa-address-card-o",
          children: [
            {
              menuName: "用户管理",
              menuIdStr: "archives/user",
              icon: "el-icon-user",
              path: "/archives/user",
            },
            {
              menuName: "客户管理",
              menuIdStr: "archives/client",
              icon: "fa fa-handshake-o",
              path: "/archives/client",
            },
            {
              menuName: "计量点管理",
              menuIdStr: "archives/jldAdmin",
              icon: "fa fa-dot-circle-o",
              path: "/archives/jldAdmin",
            },
            {
              menuName: "计量关系管理",
              menuIdStr: "archives/jldgx-manage",
              icon: "fa fa-circle-o-notch",
              path: "/archives/jldgx-manage",
            },
            {
              menuName: "运行电能表管理",
              menuIdStr: "archives/yxdnbAdmin",
              icon: "fa fa-podcast",
              path: "/archives/yxdnbAdmin",
            },
            {
              menuName: "运行互感器管理",
              menuIdStr: "archives/yxhgqAdmin",
              icon: "fa fa-american-sign-language-interpreting",
              path: "/archives/yxhgqAdmin",
            },
            {
              menuName: "供电电源管理",
              menuIdStr: "archives/gddyAdmin",
              icon: "fa fa-bolt",
              path: "/archives/gddyAdmin",
            },
          ],
        },
        {
          menuName: "抄表管理",
          menuIdStr: "6",
          icon: "el-icon-edit-outline",
          children: [
            {
              menuName: "采集管理",
              menuIdStr: "6-1",
              icon: "",
              children: [
                {
                  menuName: "采集终端管理",
                  menuIdStr: "6-1-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "终端远程调试",
                  menuIdStr: "6-1-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "采集队列管理",
                  menuIdStr: "6-1-3",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "采集任务编制",
                  menuIdStr: "6-1-4",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "抄表管理",
              menuIdStr: "6-2",
              icon: "",
              children: [
                {
                  menuName: "抄表段管理",
                  menuIdStr: "meterReading/cbdAdmin",
                  icon: "",
                  path: "/meterReading/cbdAdmin",
                },
                {
                  menuName: "区段用户管理",
                  menuIdStr: "6-2-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "示数录入管理",
                  menuIdStr: "6-2-4",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "抄表补抄管理",
                  menuIdStr: "6-2-5",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "临时抄表管理",
                  menuIdStr: "6-2-6",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "抄表计划",
              menuIdStr: "6-2-3",
              icon: "",
              path: "meterReading/cbjhAdmin",
            },
            {
              menuName: "抄表稽核",
              menuIdStr: "6-3",
              icon: "",
              children: [
                {
                  menuName: "稽核策略",
                  menuIdStr: "6-3-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "稽核设置",
                  menuIdStr: "6-3-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "示数稽核",
                  menuIdStr: "6-3-3",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "费制管理",
              menuIdStr: "6-4",
              icon: "",
              children: [
                {
                  menuName: "控制设置",
                  menuIdStr: "6-4-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "执行记录",
                  menuIdStr: "6-4-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "控制查询",
                  menuIdStr: "6-4-3",
                  icon: "",
                  path: "/test",
                },
              ],
            },
          ],
        },
        {
          menuName: "价格管理",
          menuIdStr: "7",
          icon: "fa fa-jpy",
          children: [
            {
              menuName: "价格综合管理",
              menuIdStr: "prices/all-manage",
              icon: "",
              path: "/prices/all-manage",
            },
            {
              menuName: "计算版本设置",
              menuIdStr: "prices/jsbbAdmin",
              icon: "",
              path: "/prices/jsbbAdmin",
            },
            {
              menuName: "目录电价设置",
              menuIdStr: "7-3",
              icon: "",
              children: [
                {
                  menuName: "目录电价管理",
                  menuIdStr: "prices/mldjAdmin",
                  icon: "",
                  path: "/prices/mldjAdmin",
                },
                {
                  menuName: "目录电价明细管理",
                  menuIdStr: "prices/mldjmxAdmin",
                  icon: "",
                  path: "/prices/mldjmxAdmin",
                },
              ],
            },
            {
              menuName: "代征电价设置",
              menuIdStr: "7-4",
              icon: "",
              children: [
                {
                  menuName: "代征项目管理",
                  menuIdStr: "prices/dzxmAdmin",
                  icon: "",
                  path: "/prices/dzxmAdmin",
                },
                {
                  menuName: "代征加价管理",
                  menuIdStr: "prices/dzjjAdmin",
                  icon: "",
                  path: "/prices/dzjjAdmin",
                },
                {
                  menuName: "代征电价管理",
                  menuIdStr: "prices/dzdjAdmin",
                  icon: "",
                  path: "/prices/dzdjAdmin",
                },
              ],
            },
            {
              menuName: "标准参数设置",
              menuIdStr: "7-5",
              icon: "",
              children: [
                {
                  menuName: "功率因数标准表",
                  menuIdStr: "prices/glysbzbAdmin",
                  icon: "",
                  path: "/prices/glysbzbAdmin",
                },
                {
                  menuName: "变损参数标准表",
                  menuIdStr: "prices/bscsbzbAdmin",
                  icon: "",
                  path: "/prices/bscsbzbAdmin",
                },
                {
                  menuName: "变损电量标准表",
                  menuIdStr: "prices/bsdlbzbAdmin",
                  icon: "",
                  path: "/prices/bsdlbzbAdmin",
                },
              ],
            },
            {
              menuName: "基本电价设置",
              menuIdStr: "prices/jbdjAdmin",
              icon: "",
              path: "/prices/jbdjAdmin",
            },
            {
              menuName: "执行范围设置",
              menuIdStr: "prices/zxfwAdmin",
              icon: "",
              path: "/prices/zxfwAdmin",
            },
          ],
        },
        {
          menuName: "算费管理",
          menuIdStr: "8",
          icon: "el-icon-money",
          children: [
            {
              menuName: "算费设置",
              menuIdStr: "8-1",
              icon: "",
              children: [
                {
                  menuName: "算费项目",
                  menuIdStr: "8-1-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "算费策略",
                  menuIdStr: "8-1-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "策略配置",
                  menuIdStr: "8-1-3",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "电费计算",
              menuIdStr: "8-2",
              icon: "",
              children: [
                {
                  menuName: "单户算费",
                  menuIdStr: "8-2-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "批量算费",
                  menuIdStr: "8-2-2",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "审核发行",
              menuIdStr: "8-3",
              icon: "",
              children: [
                {
                  menuName: "审核设置",
                  menuIdStr: "8-3-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "电费审核",
                  menuIdStr: "8-3-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "电费查询",
                  menuIdStr: "8-3-3",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "异常查询",
                  menuIdStr: "8-3-4",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "电费发行",
                  menuIdStr: "8-3-5",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "反向发行",
                  menuIdStr: "8-3-6",
                  icon: "",
                  path: "/test",
                },
              ],
            },
          ],
        },
        {
          menuName: "收费管理",
          menuIdStr: "9",
          icon: "fa fa-money",
          children: [
            {
              menuName: "发票管理",
              menuIdStr: "9-1",
              icon: "",
              children: [
                {
                  menuName: "发票管理",
                  menuIdStr: "9-1-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "发票审核",
                  menuIdStr: "9-1-2",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "坐收管理",
              menuIdStr: "9-2",
              icon: "",
              children: [
                {
                  menuName: "前台坐收",
                  menuIdStr: "9-2-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "收费查询",
                  menuIdStr: "9-2-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "收费冲单",
                  menuIdStr: "9-2-3",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "收费对账",
                  menuIdStr: "9-2-4",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "充值缴费",
              menuIdStr: "9-3",
              icon: "",
              children: [
                {
                  menuName: "用户充值",
                  menuIdStr: "9-3-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "用户缴费",
                  menuIdStr: "9-3-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "用户冲单",
                  menuIdStr: "9-3-3",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "缴费记录",
                  menuIdStr: "9-3-4",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "违约金管理",
              menuIdStr: "9-4",
              icon: "",
              children: [
                {
                  menuName: "计算配置",
                  menuIdStr: "9-4-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "违约金计算",
                  menuIdStr: "9-4-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "违约金减免",
                  menuIdStr: "9-4-3",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "预存扣款",
              menuIdStr: "9-5",
              icon: "",
              children: [
                {
                  menuName: "余额统计",
                  menuIdStr: "9-5-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "预存扣款",
                  menuIdStr: "9-5-2",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "收费统计",
              menuIdStr: "9-6",
              icon: "",
              children: [
                {
                  menuName: "收费日报",
                  menuIdStr: "9-6-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "流水查询",
                  menuIdStr: "9-6-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "用户查询",
                  menuIdStr: "9-6-4",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "余额查询",
                  menuIdStr: "9-6-5",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "缴费记录",
                  menuIdStr: "9-6-6",
                  icon: "",
                  path: "/test",
                },
              ],
            },
          ],
        },
        {
          menuName: "账务管理",
          menuIdStr: "10",
          icon: "el-icon-notebook-1",
          children: [
            {
              menuName: "实收管理",
              menuIdStr: "10-1",
              icon: "",
              children: [
                {
                  menuName: "实收查询",
                  menuIdStr: "10-1-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "实收日报",
                  menuIdStr: "10-1-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "实收月报",
                  menuIdStr: "10-1-3",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "应收管理",
              menuIdStr: "10-2",
              icon: "",
              children: [
                {
                  menuName: "应收查询",
                  menuIdStr: "10-2-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "应收审核",
                  menuIdStr: "10-3-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "应收月报",
                  menuIdStr: "10-2-3",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "欠费管理",
              menuIdStr: "10-3",
              icon: "",
              children: [
                {
                  menuName: "欠费查询",
                  menuIdStr: "10-3-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "欠费统计",
                  menuIdStr: "10-3-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "欠费催缴",
                  menuIdStr: "10-3-3",
                  icon: "",
                  path: "/test",
                },
              ],
            },
            {
              menuName: "报表管理",
              menuIdStr: "10-4",
              icon: "",
              children: [
                {
                  menuName: "预存日报",
                  menuIdStr: "10-4-1",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "欠费日报",
                  menuIdStr: "10-4-2",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "追收日报",
                  menuIdStr: "10-4-3",
                  icon: "",
                  path: "/test",
                },
                {
                  menuName: "综合月报",
                  menuIdStr: "10-4-4",
                  icon: "",
                  path: "/test",
                },
              ],
            },
          ],
        },
        {
          menuName: "系统管理",
          menuIdStr: "11",
          icon: "fa fa-cogs",
          children: [
            {
              menuName: "参数设置",
              menuIdStr: "11-1",
              icon: "fa fa-sliders",
              path: "/test",
            },
            {
              menuName: "角色管理",
              menuIdStr: "system/jsglAdmin",
              icon: "fa fa-pencil",
              path: "/system/jsglAdmin",
            },
            {
              menuName: "机构管理",
              menuIdStr: "system/jgglAdmin",
              icon: "fa fa-building-o",
              path: "/system/jgglAdmin",
            },
            {
              menuName: "资源管理",
              menuIdStr: "11-4",
              icon: "fa fa-suitcase",
              path: "/test",
            },
            {
              menuName: "日志查询",
              menuIdStr: "system/rzcxAdmin",
              icon: "fa fa-file-text-o",
              path: "/system/rzcxAdmin",
            },
          ],
        },
      ],
    };
  },
  methods: {
    // 跳转页面
    goPage(page) {
      // console.log(page);
      if (this.$route.path !== page.url) {
        this.$router.push(page.url);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.no-menus {
  color: #ffffff;
  padding: 15px;
}
.fa {
  vertical-align: middle;
  margin-right: 5px;
  width: 24px;
  text-align: center;
}
.box-card {
  border: none;
  height: 100vh;
  overflow: auto;
  .menu {
    height: calc(100vh - 175px);
    overflow: auto;
  }
  .often {
    z-index: 1;
    position: sticky;
    top: 0;
    color: #ffffff;
    padding: 10px 0;
    border-bottom: 1px solid #6b6b6b;
    background-color: #2f3332;
    .title {
      margin-bottom: 8px;
      padding-left: 8px;
    }
    .item {
      height: 28px;
      line-height: 28px;
      font-size: 12px;
    }
  }
}
</style>

两个组件都封装好了就只调用菜单栏容器
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

美酒没故事°

谢谢看官

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值