Skip to content

Menu 菜单

MoMenu 用于操作菜单、更多操作和多级菜单。默认把菜单面板传送到 body,避免被父容器的 overflow 裁剪。

基础用法

vue
<script setup lang="ts">
const items = [
  { label: "编辑", value: "edit", description: "修改当前记录" },
  { label: "复制", value: "copy", description: "复制一份配置" },
  { label: "删除", value: "delete", description: "移除当前记录" }
];

function handleSelect(value, item) {
  console.log(value, item);
}
</script>

<template>
  <MoMenu :items="items" @select="handleSelect">
    <template #trigger>
      <MoButton>打开菜单</MoButton>
    </template>
  </MoMenu>
</template>

二级菜单

ts
const items = [
  { label: "重命名", value: "rename" },
  {
    label: "更多操作",
    children: [
      { label: "归档", value: "archive" },
      { label: "导出", value: "export" },
      { label: "删除", value: "delete", disabled: true }
    ]
  }
];

自定义图标和内容

vue
<MoMenu :items="items">
  <template #trigger>
    <MoButton>带图标</MoButton>
  </template>

  <template #icon="{ item }">
    {{ item.icon }}
  </template>

  <template #item="{ item }">
    <span>{{ item.label }}</span>
    <span>自定义菜单项内容</span>
  </template>
</MoMenu>

位置和尺寸

vue
<MoMenu
  :items="items"
  placement="bottom-end"
  :width="220"
  :offset="10"
/>

Teleport

默认 teleported=true,菜单面板会挂到 body,适合弹层不被父容器 overflow 裁剪。需要让菜单跟随局部容器层级时,可以关闭传送。

vue
<MoMenu :items="items" :teleported="false" />

Select 事件

ts
function handleSelect(value, item, index, parent, path) {
  console.log(value, item, index, parent, path);
}

API

属性说明类型默认值
items菜单项MenuItemOption[][]
theme主题light | darklight
placement弹出位置auto | bottom-start | bottom-end | top-start | top-endauto
width菜单宽度string | number272
zIndex菜单层级number9999
teleported是否挂载到 bodybooleantrue
offset触发器和菜单间距number8
字段说明类型
label菜单项文本string
value菜单项值;未传时使用路径文本string | number
description菜单项说明string
disabled是否禁用boolean
children子菜单MenuItemOption[]

Events

事件说明回调参数
select点击叶子菜单项时触发value, item, index, parent, path

Slots

插槽说明
trigger自定义触发器
icon自定义菜单项图标,参数 { item, index, active, parent, path, level }
item自定义菜单项内容,参数 { item, index, active, parent, path, level, hasChildren }
submenu-arrow自定义子菜单箭头