vue2 + element三级菜单实现模板

需求:

需要一个含有三级菜单的结构模板,用于业务快速开发。

解决:

sidebar.vue

复制代码
<template>
  <el-menu :default-active="defaultActive" class="el-menu-vertical-demo" 
    active-text-color="#ffd04b">
    <template v-for="menu in menus">
      <el-submenu :index="menu.index" v-if="menu.children && menu.children.length > 0">
        <template slot="title">
          <i class="el-icon-menu"></i>
          <span>{{ menu.name }}</span>
        </template>
        <template v-for="child in menu.children">
          <el-submenu :index="child.index" v-if="child.children && child.children.length > 0">
            <template slot="title">{{ child.name }}</template>
            <el-menu-item v-for="subChild in child.children" :index="subChild.index" :key="subChild.index">{{
    subChild.name
  }}</el-menu-item>
          </el-submenu>
          <el-menu-item v-else :index="child.index">{{ child.name }}</el-menu-item>
        </template>
      </el-submenu>
      <el-menu-item v-else :index="menu.index">{{ menu.name }}</el-menu-item>
    </template>
  </el-menu>
</template>

<script>
export default {
  data() {
    return {
      defaultActive: '1',
      menus: [
        {
          index: '1',
          name: '一级菜单1',
          children: [
            {
              index: '1-1',
              name: '二级菜单1-1',
              children: [
                { index: '1-1-1', name: '三级菜单1-1-1' },
                { index: '1-1-2', name: '三级菜单1-1-2' }
              ]
            },
            {
              index: '1-2',
              name: '二级菜单1-2',
              children: [
                { index: '1-2-1', name: '三级菜单1-2-1' },
                { index: '1-2-2', name: '三级菜单1-2-2' }
              ]
            }
          ]
        },
        {
          index: '2',
          name: '一级菜单2',
          children: [
            {
              index: '2-1',
              name: '二级菜单2-1',
              children: [
                { index: '2-1-1', name: '三级菜单2-1-1' },
                { index: '2-1-2', name: '三级菜单2-1-2' }
              ]
            }
          ]
        }
      ]
    };
  }
};
</script>

<style lang="less" scoped>
.el-menu {
  width: 287px;

  .el-menu-item {
    font-size: 16px;
    padding-left: 49px;

    &.is-active {
      //color: #fff;
      //background-color: #6c7c97;
      background-color: #fafafa;
      color: #409eff;
    }
  }
}

.el-submenu /deep/ .el-submenu__title {
  font-size: 16px;
}

.cuscRouter {
  // background-color: #fafafa;
  color: #409eff !important;
}
</style>
相关推荐
小小小小宇1 小时前
浏览器自动化实战:把「打开掘金写一篇文章」变成可复用的 Skill
vue.js·自动化运维
htzyl2061 小时前
金额计算:为什么前端不该用浮点数存钱
前端·javascript·算法
Iseeyore1 小时前
Vue3 Vs React
前端·vue.js
map_3d_vis3 小时前
百度地图 Hello World:5 分钟用 JSAPI 4.0 显示一张地图
前端·javascript·地图·百度地图
不要试图纠正别人3 小时前
构建 0 报错,产物体积翻倍:换引擎最容易漏的对账
前端·javascript
Hilaku4 小时前
面了 30 家公司拿到 2 个 Offer!
前端·javascript·程序员
CappuccinoRose5 小时前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互
1-1=06 小时前
Vue 混入(Mixin)和 2.插件 (Plugins)
前端·vue.js
用户298698530146 小时前
React 前端实现 Excel 与 OpenXML 双向转换:基于 WebAssembly 的浏览器端方案
javascript·react.js·excel
星落梦生7 小时前
Tlias前端案例涉及知识点 (14)
开发语言·前端·javascript