NavMenu 导航菜单

为网站提供导航功能的菜单。

顶栏

适用广泛的基础用法。

NavMenu 导航菜单 - 图1

导航菜单默认为垂直模式,通过mode属性可以使导航菜单变更为水平模式。另外,在菜单中通过submenu组件可以生成二级菜单。Menu 还提供了background-colortext-coloractive-text-color,分别用于设置菜单的背景色、菜单的文字颜色和当前激活菜单的文字颜色。

  1. <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
  2. <el-menu-item index="1">处理中心</el-menu-item>
  3. <el-submenu index="2">
  4. <template slot="title">我的工作台</template>
  5. <el-menu-item index="2-1">选项1</el-menu-item>
  6. <el-menu-item index="2-2">选项2</el-menu-item>
  7. <el-menu-item index="2-3">选项3</el-menu-item>
  8. <el-submenu index="2-4">
  9. <template slot="title">选项4</template>
  10. <el-menu-item index="2-4-1">选项1</el-menu-item>
  11. <el-menu-item index="2-4-2">选项2</el-menu-item>
  12. <el-menu-item index="2-4-3">选项3</el-menu-item>
  13. </el-submenu>
  14. </el-submenu>
  15. <el-menu-item index="3" disabled>消息中心</el-menu-item>
  16. <el-menu-item index="4"><a href="https://www.ele.me" target="_blank">订单管理</a></el-menu-item>
  17. </el-menu>
  18. <div class="line"></div>
  19. <el-menu
  20. :default-active="activeIndex2"
  21. class="el-menu-demo"
  22. mode="horizontal"
  23. @select="handleSelect"
  24. background-color="#545c64"
  25. text-color="#fff"
  26. active-text-color="#ffd04b">
  27. <el-menu-item index="1">处理中心</el-menu-item>
  28. <el-submenu index="2">
  29. <template slot="title">我的工作台</template>
  30. <el-menu-item index="2-1">选项1</el-menu-item>
  31. <el-menu-item index="2-2">选项2</el-menu-item>
  32. <el-menu-item index="2-3">选项3</el-menu-item>
  33. <el-submenu index="2-4">
  34. <template slot="title">选项4</template>
  35. <el-menu-item index="2-4-1">选项1</el-menu-item>
  36. <el-menu-item index="2-4-2">选项2</el-menu-item>
  37. <el-menu-item index="2-4-3">选项3</el-menu-item>
  38. </el-submenu>
  39. </el-submenu>
  40. <el-menu-item index="3" disabled>消息中心</el-menu-item>
  41. <el-menu-item index="4"><a href="https://www.ele.me" target="_blank">订单管理</a></el-menu-item>
  42. </el-menu>
  43. <script>
  44. export default {
  45. data() {
  46. return {
  47. activeIndex: '1',
  48. activeIndex2: '1'
  49. };
  50. },
  51. methods: {
  52. handleSelect(key, keyPath) {
  53. console.log(key, keyPath);
  54. }
  55. }
  56. }
  57. </script>

侧栏

垂直菜单,可内嵌子菜单。

NavMenu 导航菜单 - 图2

通过el-menu-item-group组件可以实现菜单进行分组,分组名可以通过title属性直接设定,也可以通过具名 slot 来设定。

<el-row class="tac">
  <el-col :span="12">
    <h5>默认颜色</h5>
    <el-menu
      default-active="2"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose">
      <el-submenu index="1">
        <template slot="title">
          <i class="el-icon-location"></i>
          <span>导航一</span>
        </template>
        <el-menu-item-group>
          <template slot="title">分组一</template>
          <el-menu-item index="1-1">选项1</el-menu-item>
          <el-menu-item index="1-2">选项2</el-menu-item>
        </el-menu-item-group>
        <el-menu-item-group title="分组2">
          <el-menu-item index="1-3">选项3</el-menu-item>
        </el-menu-item-group>
        <el-submenu index="1-4">
          <template slot="title">选项4</template>
          <el-menu-item index="1-4-1">选项1</el-menu-item>
        </el-submenu>
      </el-submenu>
      <el-menu-item index="2">
        <i class="el-icon-menu"></i>
        <span slot="title">导航二</span>
      </el-menu-item>
      <el-menu-item index="3" disabled>
        <i class="el-icon-document"></i>
        <span slot="title">导航三</span>
      </el-menu-item>
      <el-menu-item index="4">
        <i class="el-icon-setting"></i>
        <span slot="title">导航四</span>
      </el-menu-item>
    </el-menu>
  </el-col>
  <el-col :span="12">
    <h5>自定义颜色</h5>
    <el-menu
      default-active="2"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b">
      <el-submenu index="1">
        <template slot="title">
          <i class="el-icon-location"></i>
          <span>导航一</span>
        </template>
        <el-menu-item-group>
          <template slot="title">分组一</template>
          <el-menu-item index="1-1">选项1</el-menu-item>
          <el-menu-item index="1-2">选项2</el-menu-item>
        </el-menu-item-group>
        <el-menu-item-group title="分组2">
          <el-menu-item index="1-3">选项3</el-menu-item>
        </el-menu-item-group>
        <el-submenu index="1-4">
          <template slot="title">选项4</template>
          <el-menu-item index="1-4-1">选项1</el-menu-item>
        </el-submenu>
      </el-submenu>
      <el-menu-item index="2">
        <i class="el-icon-menu"></i>
        <span slot="title">导航二</span>
      </el-menu-item>
      <el-menu-item index="3" disabled>
        <i class="el-icon-document"></i>
        <span slot="title">导航三</span>
      </el-menu-item>
      <el-menu-item index="4">
        <i class="el-icon-setting"></i>
        <span slot="title">导航四</span>
      </el-menu-item>
    </el-menu>
  </el-col>
</el-row>

<script>
  export default {
    methods: {
      handleOpen(key, keyPath) {
        console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        console.log(key, keyPath);
      }
    }
  }
</script>

折叠

NavMenu 导航菜单 - 图3

<el-radio-group v-model="isCollapse" style="margin-bottom: 20px;">
  <el-radio-button :label="false">展开</el-radio-button>
  <el-radio-button :label="true">收起</el-radio-button>
</el-radio-group>
<el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose" :collapse="isCollapse">
  <el-submenu index="1">
    <template slot="title">
      <i class="el-icon-location"></i>
      <span slot="title">导航一</span>
    </template>
    <el-menu-item-group>
      <span slot="title">分组一</span>
      <el-menu-item index="1-1">选项1</el-menu-item>
      <el-menu-item index="1-2">选项2</el-menu-item>
    </el-menu-item-group>
    <el-menu-item-group title="分组2">
      <el-menu-item index="1-3">选项3</el-menu-item>
    </el-menu-item-group>
    <el-submenu index="1-4">
      <span slot="title">选项4</span>
      <el-menu-item index="1-4-1">选项1</el-menu-item>
    </el-submenu>
  </el-submenu>
  <el-menu-item index="2">
    <i class="el-icon-menu"></i>
    <span slot="title">导航二</span>
  </el-menu-item>
  <el-menu-item index="3" disabled>
    <i class="el-icon-document"></i>
    <span slot="title">导航三</span>
  </el-menu-item>
  <el-menu-item index="4">
    <i class="el-icon-setting"></i>
    <span slot="title">导航四</span>
  </el-menu-item>
</el-menu>

<style>
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
</style>

<script>
  export default {
    data() {
      return {
        isCollapse: true
      };
    },
    methods: {
      handleOpen(key, keyPath) {
        console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        console.log(key, keyPath);
      }
    }
  }
</script>

Menu Attribute

参数说明类型可选值默认值
mode模式stringhorizontal / verticalvertical
collapse是否水平折叠收起菜单(仅在 mode 为 vertical 时可用)booleanfalse
background-color菜单的背景色(仅支持 hex 格式)string#ffffff
text-color菜单的文字颜色(仅支持 hex 格式)string#303133
active-text-color当前激活菜单的文字颜色(仅支持 hex 格式)string#409EFF
default-active当前激活菜单的 indexstring
default-openeds当前打开的 sub-menu 的 index 的数组Array
unique-opened是否只保持一个子菜单的展开booleanfalse
menu-trigger子菜单打开的触发方式(只在 mode 为 horizontal 时有效)stringhover / clickhover
router是否使用 vue-router 的模式,启用该模式会在激活导航时以 index 作为 path 进行路由跳转booleanfalse
collapse-transition是否开启折叠动画booleantrue

Menu Methods

方法名称说明参数
open展开指定的 sub-menuindex: 需要打开的 sub-menu 的 index
close收起指定的 sub-menuindex: 需要收起的 sub-menu 的 index

Menu Events

事件名称说明回调参数
select菜单激活回调index: 选中菜单项的 index, indexPath: 选中菜单项的 index path
opensub-menu 展开的回调index: 打开的 sub-menu 的 index, indexPath: 打开的 sub-menu 的 index path
closesub-menu 收起的回调index: 收起的 sub-menu 的 index, indexPath: 收起的 sub-menu 的 index path

SubMenu Attribute

参数说明类型可选值默认值
index唯一标志string/nullnull
popper-class弹出菜单的自定义类名string
show-timeout展开 sub-menu 的延时number300
hide-timeout收起 sub-menu 的延时number300
disabled是否禁用booleanfalse
popper-append-to-body是否将弹出菜单插入至 body 元素。在菜单的定位出现问题时,可尝试修改该属性boolean一级子菜单:true / 非一级子菜单:false

Menu-Item Attribute

参数说明类型可选值默认值
index唯一标志string
routeVue Router 路径对象Object
disabled是否禁用booleanfalse

Menu-Group Attribute

参数说明类型可选值默认值
title分组标题string