Dropdown下拉菜单 - 图1

Dropdown 下拉菜单

向下弹出的列表。

何时使用

当页面上的操作命令过多时,用此组件可以收纳操作元素。点击或移入触点,会出现一个下拉菜单。可在列表中进行选择,并执行相应的命令。

  • 用于收罗一组命令操作。
  • Select 用于选择,而 Dropdown 是命令集合。

代码演示

Hover me Dropdown下拉菜单 - 图2

基本

最简单的下拉菜单。

  1. <template>
  2. <a-dropdown>
  3. <a class="ant-dropdown-link" @click.prevent>
  4. Hover me
  5. <DownOutlined />
  6. </a>
  7. <template #overlay>
  8. <a-menu>
  9. <a-menu-item>
  10. <a href="javascript:;">1st menu item</a>
  11. </a-menu-item>
  12. <a-menu-item>
  13. <a href="javascript:;">2nd menu item</a>
  14. </a-menu-item>
  15. <a-menu-item>
  16. <a href="javascript:;">3rd menu item</a>
  17. </a-menu-item>
  18. </a-menu>
  19. </template>
  20. </a-dropdown>
  21. </template>
  22. <script lang="ts">
  23. import { defineComponent } from 'vue';
  24. import { DownOutlined } from '@ant-design/icons-vue';
  25. export default defineComponent({
  26. components: {
  27. DownOutlined,
  28. },
  29. });
  30. </script>

Dropdown下拉菜单 - 图3

带下拉框的按钮

左边是按钮,右边是额外的相关功能菜单。可设置 icon 属性来修改右边的图标。

  1. <template>
  2. <div class="demo-dropdown-wrap">
  3. <a-dropdown-button @click="handleButtonClick">
  4. Dropdown
  5. <template #overlay>
  6. <a-menu @click="handleMenuClick">
  7. <a-menu-item key="1">
  8. <UserOutlined />
  9. 1st menu item
  10. </a-menu-item>
  11. <a-menu-item key="2">
  12. <UserOutlined />
  13. 2nd menu item
  14. </a-menu-item>
  15. <a-menu-item key="3">
  16. <UserOutlined />
  17. 3rd item
  18. </a-menu-item>
  19. </a-menu>
  20. </template>
  21. </a-dropdown-button>
  22. <a-dropdown-button>
  23. Dropdown
  24. <template #overlay>
  25. <a-menu @click="handleMenuClick">
  26. <a-menu-item key="1">
  27. <UserOutlined />
  28. 1st menu item
  29. </a-menu-item>
  30. <a-menu-item key="2">
  31. <UserOutlined />
  32. 2nd menu item
  33. </a-menu-item>
  34. <a-menu-item key="3">
  35. <UserOutlined />
  36. 3rd item
  37. </a-menu-item>
  38. </a-menu>
  39. </template>
  40. <template #icon><UserOutlined /></template>
  41. </a-dropdown-button>
  42. <a-dropdown-button disabled @click="handleButtonClick">
  43. Dropdown
  44. <template #overlay>
  45. <a-menu @click="handleMenuClick">
  46. <a-menu-item key="1">
  47. <UserOutlined />
  48. 1st menu item
  49. </a-menu-item>
  50. <a-menu-item key="2">
  51. <UserOutlined />
  52. 2nd menu item
  53. </a-menu-item>
  54. <a-menu-item key="3">
  55. <UserOutlined />
  56. 3rd item
  57. </a-menu-item>
  58. </a-menu>
  59. </template>
  60. </a-dropdown-button>
  61. <a-dropdown>
  62. <template #overlay>
  63. <a-menu @click="handleMenuClick">
  64. <a-menu-item key="1">
  65. <UserOutlined />
  66. 1st menu item
  67. </a-menu-item>
  68. <a-menu-item key="2">
  69. <UserOutlined />
  70. 2nd menu item
  71. </a-menu-item>
  72. <a-menu-item key="3">
  73. <UserOutlined />
  74. 3rd item
  75. </a-menu-item>
  76. </a-menu>
  77. </template>
  78. <a-button>
  79. Button
  80. <DownOutlined />
  81. </a-button>
  82. </a-dropdown>
  83. </div>
  84. </template>
  85. <script lang="ts">
  86. import { defineComponent } from 'vue';
  87. import { UserOutlined, DownOutlined } from '@ant-design/icons-vue';
  88. export default defineComponent({
  89. setup() {
  90. const handleButtonClick = (e: Event) => {
  91. console.log('click left button', e);
  92. };
  93. const handleMenuClick = (e: Event) => {
  94. console.log('click', e);
  95. };
  96. return {
  97. handleButtonClick,
  98. handleMenuClick,
  99. };
  100. },
  101. components: {
  102. UserOutlined,
  103. DownOutlined,
  104. },
  105. });
  106. </script>
  107. <style lang="less" scoped>
  108. .demo-dropdown-wrap :deep(.ant-dropdown-button) {
  109. margin-right: 8px;
  110. margin-bottom: 8px;
  111. }
  112. </style>

Hover me Dropdown下拉菜单 - 图4

其他元素

分割线和不可用菜单项。

  1. <template>
  2. <a-dropdown>
  3. <a class="ant-dropdown-link" @click.prevent>
  4. Hover me
  5. <DownOutlined />
  6. </a>
  7. <template #overlay>
  8. <a-menu>
  9. <a-menu-item key="0">
  10. <a target="_blank" rel="noopener noreferrer" href="http://www.alipay.com/">
  11. 1st menu item
  12. </a>
  13. </a-menu-item>
  14. <a-menu-item key="1">
  15. <a target="_blank" rel="noopener noreferrer" href="http://www.taobao.com/">
  16. 2nd menu item
  17. </a>
  18. </a-menu-item>
  19. <a-menu-divider />
  20. <a-menu-item key="3" disabled>3rd menu item(disabled)</a-menu-item>
  21. </a-menu>
  22. </template>
  23. </a-dropdown>
  24. </template>
  25. <script lant="ts">
  26. import { defineComponent } from 'vue';
  27. import { DownOutlined } from '@ant-design/icons-vue';
  28. export default defineComponent({
  29. components: {
  30. DownOutlined,
  31. },
  32. });
  33. </script>

Dropdown下拉菜单 - 图5

弹出位置

支持 6 个弹出位置。

  1. <template>
  2. <div id="components-dropdown-demo-placement">
  3. <template v-for="(placement, index) in placements" :key="placement">
  4. <a-dropdown :placement="placement">
  5. <a-button>{{ placement }}</a-button>
  6. <template #overlay>
  7. <a-menu>
  8. <a-menu-item>
  9. <a target="_blank" rel="noopener noreferrer" href="http://www.alipay.com/">
  10. 1st menu item
  11. </a>
  12. </a-menu-item>
  13. <a-menu-item>
  14. <a target="_blank" rel="noopener noreferrer" href="http://www.taobao.com/">
  15. 2nd menu item
  16. </a>
  17. </a-menu-item>
  18. <a-menu-item>
  19. <a target="_blank" rel="noopener noreferrer" href="http://www.tmall.com/">
  20. 3rd menu item
  21. </a>
  22. </a-menu-item>
  23. </a-menu>
  24. </template>
  25. </a-dropdown>
  26. <br v-if="index === 2" />
  27. </template>
  28. </div>
  29. </template>
  30. <script lang="ts">
  31. import { defineComponent } from 'vue';
  32. export default defineComponent({
  33. setup() {
  34. return {
  35. placements: ['bottomLeft', 'bottomCenter', 'bottomRight', 'topLeft', 'topCenter', 'topRight'],
  36. };
  37. },
  38. });
  39. </script>
  40. <style>
  41. #components-dropdown-demo-placement .ant-btn {
  42. margin-right: 8px;
  43. margin-bottom: 8px;
  44. }
  45. </style>

Click me Dropdown下拉菜单 - 图6

触发方式

默认是移入触发菜单,可以点击触发。

  1. <template>
  2. <a-dropdown :trigger="['click']">
  3. <a class="ant-dropdown-link" @click.prevent>
  4. Click me
  5. <DownOutlined />
  6. </a>
  7. <template #overlay>
  8. <a-menu>
  9. <a-menu-item key="0">
  10. <a href="http://www.alipay.com/">1st menu item</a>
  11. </a-menu-item>
  12. <a-menu-item key="1">
  13. <a href="http://www.taobao.com/">2nd menu item</a>
  14. </a-menu-item>
  15. <a-menu-divider />
  16. <a-menu-item key="3">3rd menu item</a-menu-item>
  17. </a-menu>
  18. </template>
  19. </a-dropdown>
  20. </template>
  21. <script lang="ts">
  22. import { defineComponent } from 'vue';
  23. import { DownOutlined } from '@ant-design/icons-vue';
  24. export default defineComponent({
  25. components: {
  26. DownOutlined,
  27. },
  28. });
  29. </script>

Dropdown下拉菜单 - 图7

右键菜单

默认是移入触发菜单,可以点击鼠标右键触发。

  1. <template>
  2. <a-dropdown :trigger="['contextmenu']">
  3. <div
  4. :style="{
  5. textAlign: 'center',
  6. background: '#f7f7f7',
  7. height: '200px',
  8. lineHeight: '200px',
  9. color: '#777',
  10. }"
  11. >
  12. Right Click on here
  13. </div>
  14. <template #overlay>
  15. <a-menu>
  16. <a-menu-item key="1">1st menu item</a-menu-item>
  17. <a-menu-item key="2">2nd menu item</a-menu-item>
  18. <a-menu-item key="3">3rd menu item</a-menu-item>
  19. </a-menu>
  20. </template>
  21. </a-dropdown>
  22. </template>

Hover me, Click menu item Dropdown下拉菜单 - 图8

触发事件

点击菜单项后会触发事件,用户可以通过相应的菜单项 key 进行不同的操作。

  1. <template>
  2. <a-dropdown>
  3. <a class="ant-dropdown-link" @click.prevent>
  4. Hover me, Click menu item
  5. <DownOutlined />
  6. </a>
  7. <template #overlay>
  8. <a-menu @click="onClick">
  9. <a-menu-item key="1">1st menu item</a-menu-item>
  10. <a-menu-item key="2">2nd menu item</a-menu-item>
  11. <a-menu-item key="3">3rd menu item</a-menu-item>
  12. </a-menu>
  13. </template>
  14. </a-dropdown>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, VNodeChild } from 'vue';
  18. import { DownOutlined } from '@ant-design/icons-vue';
  19. interface MenuInfo {
  20. key: string;
  21. keyPath: string[];
  22. item: VNodeChild;
  23. domEvent: MouseEvent;
  24. }
  25. export default defineComponent({
  26. setup() {
  27. const onClick = ({ key }: MenuInfo) => {
  28. console.log(`Click on item ${key}`);
  29. };
  30. return {
  31. onClick,
  32. };
  33. },
  34. components: {
  35. DownOutlined,
  36. },
  37. });
  38. </script>

Hover me Dropdown下拉菜单 - 图9

菜单隐藏方式

默认是点击关闭菜单,可以关闭此功能。

  1. <template>
  2. <a-dropdown v-model:visible="visible">
  3. <a class="ant-dropdown-link" @click.prevent>
  4. Hover me
  5. <DownOutlined />
  6. </a>
  7. <template #overlay>
  8. <a-menu @click="handleMenuClick">
  9. <a-menu-item key="1">Clicking me will not close the menu.</a-menu-item>
  10. <a-menu-item key="2">Clicking me will not close the menu also.</a-menu-item>
  11. <a-menu-item key="3">Clicking me will close the menu</a-menu-item>
  12. </a-menu>
  13. </template>
  14. </a-dropdown>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, ref, VNodeChild } from 'vue';
  18. import { DownOutlined } from '@ant-design/icons-vue';
  19. interface MenuInfo {
  20. key: string;
  21. keyPath: string[];
  22. item: VNodeChild;
  23. domEvent: MouseEvent;
  24. }
  25. export default defineComponent({
  26. setup() {
  27. const visible = ref(false);
  28. const handleMenuClick = (e: MenuInfo) => {
  29. if (e.key === '3') {
  30. visible.value = false;
  31. }
  32. };
  33. return {
  34. visible,
  35. handleMenuClick,
  36. };
  37. },
  38. components: {
  39. DownOutlined,
  40. },
  41. });
  42. </script>

Cascading menu Dropdown下拉菜单 - 图10

多级菜单

传入的菜单里有多个层级。

  1. <template>
  2. <a-dropdown>
  3. <a class="ant-dropdown-link" @click.prevent>
  4. Cascading menu
  5. <DownOutlined />
  6. </a>
  7. <template #overlay>
  8. <a-menu>
  9. <a-menu-item>1st menu item</a-menu-item>
  10. <a-menu-item>2nd menu item</a-menu-item>
  11. <a-sub-menu key="test" title="sub menu">
  12. <a-menu-item>3rd menu item</a-menu-item>
  13. <a-menu-item>4th menu item</a-menu-item>
  14. </a-sub-menu>
  15. <a-sub-menu title="disabled sub menu" disabled>
  16. <a-menu-item>5d menu item</a-menu-item>
  17. <a-menu-item>6th menu item</a-menu-item>
  18. </a-sub-menu>
  19. </a-menu>
  20. </template>
  21. </a-dropdown>
  22. </template>
  23. <script lang="ts">
  24. import { defineComponent } from 'vue';
  25. import { DownOutlined } from '@ant-design/icons-vue';
  26. export default defineComponent({
  27. components: {
  28. DownOutlined,
  29. },
  30. });
  31. </script>

API

属性如下

参数说明类型默认值
disabled菜单是否禁用boolean-
getPopupContainer菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。Function(triggerNode)() => document.body
overlay(v-slot)菜单Menu-
overlayClassName下拉根元素的类名称string-
overlayStyle下拉根元素的样式object-
placement菜单弹出位置:bottomLeft bottomCenter bottomRight topLeft topCenter topRightStringbottomLeft
trigger触发下拉的行为, 移动端不支持 hoverArray<click|hover|contextmenu>[‘hover’]
visible(v-model)菜单是否显示boolean-

overlay 菜单使用 Menu,还包括菜单项 Menu.Item,分割线 Menu.Divider

注意: Menu.Item 必须设置唯一的 key 属性。

Dropdown 下的 Menu 默认不可选中。如果需要菜单可选中,可以指定 <Menu selectable>.

事件

事件名称说明回调参数
visibleChange菜单显示状态改变时调用,参数为 visiblefunction(visible)

Dropdown.Button

参数说明类型默认值版本
disabled菜单是否禁用boolean-
icon右侧的 iconVNode | slot-1.5.0
overlay(v-slot)菜单Menu-
placement菜单弹出位置:bottomLeft bottomCenter bottomRight topLeft topCenter topRightStringbottomLeft
size按钮大小,和 Button 一致string‘default’
trigger触发下拉的行为Array<click|hover|contextmenu>[‘hover’]
type按钮类型,和 Button 一致string‘default’
visible(v-model)菜单是否显示boolean-

Dropdown.Button 事件

事件名称说明回调参数
click点击左侧按钮的回调,和 Button 一致Function
visibleChange菜单显示状态改变时调用,参数为 visiblefunction(visible)