菜单

v-menu 组件在激活它的元素的位置上展示一个菜单。

用例

请记住将激活菜单的元素放置在 activator 插槽中。

template script


  1. <template>
  2. <div class="text-center">
  3. <v-menu offset-y>
  4. <template v-slot:activator="{ on }">
  5. <v-btn
  6. color="primary"
  7. dark
  8. v-on="on"
  9. >
  10. Dropdown
  11. </v-btn>
  12. </template>
  13. <v-list>
  14. <v-list-item
  15. v-for="(item, index) in items"
  16. :key="index"
  17. @click=""
  18. >
  19. <v-list-item-title>{{ item.title }}</v-list-item-title>
  20. </v-list-item>
  21. </v-list>
  22. </v-menu>
  23. </div>
  24. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. }),
  11. }
  12. </script>

Menus(菜单) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

Menus(菜单) - 图2

实战场

template script


  1. <template>
  2. <v-row align="center">
  3. <v-row justify="space-around">
  4. <v-switch v-model="disabled" class="ma-2" label="Disabled"></v-switch>
  5. <v-switch v-model="absolute" class="ma-2" label="Absolute"></v-switch>
  6. <v-switch v-model="openOnHover" class="ma-2" label="Open on hover"></v-switch>
  7. <v-switch v-model="closeOnClick" label="Close on click"></v-switch>
  8. <v-switch v-model="closeOnContentClick" label="Close on content click"></v-switch>
  9. <v-switch v-model="offsetX" label="X offset"></v-switch>
  10. <v-switch v-model="offsetY" label="Y offset"></v-switch>
  11. <v-switch v-model="value" class="ma-2" label="Value"></v-switch>
  12. </v-row>
  13. <v-menu
  14. v-model="value"
  15. :disabled="disabled"
  16. :absolute="absolute"
  17. :open-on-hover="openOnHover"
  18. :close-on-click="closeOnClick"
  19. :close-on-content-click="closeOnContentClick"
  20. :offset-x="offsetX"
  21. :offset-y="offsetY"
  22. >
  23. <template v-slot:activator="{ on }">
  24. <v-btn
  25. color="primary"
  26. dark
  27. v-on="on"
  28. >
  29. Dropdown
  30. </v-btn>
  31. </template>
  32. <v-list>
  33. <v-list-item
  34. v-for="(item, index) in items"
  35. :key="index"
  36. @click=""
  37. >
  38. <v-list-item-title>{{ item.title }}</v-list-item-title>
  39. </v-list-item>
  40. </v-list>
  41. </v-menu>
  42. </v-row>
  43. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. disabled: false,
  11. absolute: false,
  12. openOnHover: false,
  13. value: false,
  14. closeOnClick: true,
  15. closeOnContentClick: true,
  16. offsetX: false,
  17. offsetY: true,
  18. }),
  19. }
  20. </script>

Menus(菜单) - 图3

示例

下面是一些简单到复杂的例子。

绝对定位

菜单也可以使用absolute属性绝对定位在激活元素的顶部。尝试点击图片上的任何地方。

template script


  1. <template>
  2. <v-row
  3. class="d-flex"
  4. justify="center"
  5. >
  6. <v-menu
  7. v-model="showMenu"
  8. absolute
  9. offset-y
  10. style="max-width: 600px"
  11. >
  12. <template v-slot:activator="{ on }">
  13. <v-card
  14. class="portrait"
  15. img="https://cdn.vuetifyjs.com/images/cards/girl.jpg"
  16. height="300"
  17. width="600"
  18. v-on="on"
  19. ></v-card>
  20. </template>
  21. <v-list>
  22. <v-list-item
  23. v-for="(item, index) in items"
  24. :key="index"
  25. @click=""
  26. >
  27. <v-list-item-title>{{ item.title }}</v-list-item-title>
  28. </v-list-item>
  29. </v-list>
  30. </v-menu>
  31. </v-row>
  32. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. showMenu: false,
  5. items: [
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me' },
  9. { title: 'Click Me 2' },
  10. ],
  11. }),
  12. }
  13. </script>

Menus(菜单) - 图4

菜单带有激活器和提示

在新的 v-slot 语法中,嵌套的激活器(比如与同一个激活器按钮相连的 v-menuv-tooltip)需要特殊的设置才能正常工作。注意:同样的语法也适用于其他嵌套的激活器,比如 v-dialog w/ v-tooltip

template script


  1. <template>
  2. <div class="text-center">
  3. <v-menu>
  4. <template v-slot:activator="{ on: menu }">
  5. <v-tooltip bottom>
  6. <template v-slot:activator="{ on: tooltip }">
  7. <v-btn
  8. color="primary"
  9. dark
  10. v-on="{ ...tooltip, ...menu }"
  11. >Dropdown w/ Tooltip</v-btn>
  12. </template>
  13. <span>Im A ToolTip</span>
  14. </v-tooltip>
  15. </template>
  16. <v-list>
  17. <v-list-item
  18. v-for="(item, index) in items"
  19. :key="index"
  20. @click=""
  21. >
  22. <v-list-item-title>{{ item.title }}</v-list-item-title>
  23. </v-list-item>
  24. </v-list>
  25. </v-menu>
  26. </div>
  27. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me1' },
  6. { title: 'Click Me2' },
  7. { title: 'Click Me3' },
  8. { title: 'Click Me4' },
  9. ],
  10. }),
  11. }
  12. </script>

Menus(菜单) - 图5

悬停

菜单可以使用悬停进行访问,而不是点击 open-on-hover 属性。

template script


  1. <template>
  2. <div class="text-center">
  3. <v-menu open-on-hover top offset-y>
  4. <template v-slot:activator="{ on }">
  5. <v-btn
  6. color="primary"
  7. dark
  8. v-on="on"
  9. >
  10. Dropdown
  11. </v-btn>
  12. </template>
  13. <v-list>
  14. <v-list-item
  15. v-for="(item, index) in items"
  16. :key="index"
  17. @click=""
  18. >
  19. <v-list-item-title>{{ item.title }}</v-list-item-title>
  20. </v-list-item>
  21. </v-list>
  22. </v-menu>
  23. </div>
  24. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. }),
  11. }
  12. </script>

Menus(菜单) - 图6

自定义过渡

Vuetify 带有3个标准过渡,scaleslide-xslide-y。您也可以创建自己的并将其作为过渡参数传递。有关如何构建内置过渡的示例,请访问 here

template script


  1. <template>
  2. <v-row justify="space-around">
  3. <v-menu
  4. bottom
  5. origin="center center"
  6. transition="scale-transition"
  7. >
  8. <template v-slot:activator="{ on }">
  9. <v-btn
  10. color="primary"
  11. dark
  12. v-on="on"
  13. >
  14. Scale Transition
  15. </v-btn>
  16. </template>
  17. <v-list>
  18. <v-list-item
  19. v-for="(item, i) in items"
  20. :key="i"
  21. @click=""
  22. >
  23. <v-list-item-title>{{ item.title }}</v-list-item-title>
  24. </v-list-item>
  25. </v-list>
  26. </v-menu>
  27. <v-menu
  28. transition="slide-x-transition"
  29. bottom
  30. right
  31. >
  32. <template v-slot:activator="{ on }">
  33. <v-btn
  34. class="deep-orange"
  35. color="primary"
  36. dark
  37. v-on="on"
  38. >
  39. Slide X Transition
  40. </v-btn>
  41. </template>
  42. <v-list>
  43. <v-list-item
  44. v-for="(item, i) in items"
  45. :key="i"
  46. @click=""
  47. >
  48. <v-list-item-title>{{ item.title }}</v-list-item-title>
  49. </v-list-item>
  50. </v-list>
  51. </v-menu>
  52. <v-menu
  53. transition="slide-y-transition"
  54. bottom
  55. >
  56. <template v-slot:activator="{ on }">
  57. <v-btn
  58. class="purple"
  59. color="primary"
  60. dark
  61. v-on="on"
  62. >
  63. Slide Y Transition
  64. </v-btn>
  65. </template>
  66. <v-list>
  67. <v-list-item
  68. v-for="(item, i) in items"
  69. :key="i"
  70. @click=""
  71. >
  72. <v-list-item-title>{{ item.title }}</v-list-item-title>
  73. </v-list-item>
  74. </v-list>
  75. </v-menu>
  76. </v-row>
  77. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. }),
  11. }
  12. </script>

Menus(菜单) - 图7

禁用

您可以禁用菜单。 禁用的菜单无法打开。

template script


  1. <template>
  2. <div class="text-center">
  3. <v-menu disabled top offset-y>
  4. <template v-slot:activator="{ on }">
  5. <v-btn
  6. color="primary"
  7. dark
  8. v-on="on"
  9. >
  10. Dropdown
  11. </v-btn>
  12. </template>
  13. <v-list>
  14. <v-list-item
  15. v-for="(item, index) in items"
  16. :key="index"
  17. @click=""
  18. >
  19. <v-list-item-title>{{ item.title }}</v-list-item-title>
  20. </v-list-item>
  21. </v-list>
  22. </v-menu>
  23. </div>
  24. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. }),
  11. }
  12. </script>

Menus(菜单) - 图8

X 偏移

菜单可以通过 X 轴偏移,以使激活器可见。

template script


  1. <template>
  2. <div class="text-center">
  3. <v-switch v-model="offset" label="X offset"></v-switch>
  4. <v-menu top :offset-x="offset">
  5. <template v-slot:activator="{ on }">
  6. <v-btn
  7. color="primary"
  8. dark
  9. v-on="on"
  10. >
  11. Dropdown
  12. </v-btn>
  13. </template>
  14. <v-list>
  15. <v-list-item
  16. v-for="(item, index) in items"
  17. :key="index"
  18. @click=""
  19. >
  20. <v-list-item-title>{{ item.title }}</v-list-item-title>
  21. </v-list-item>
  22. </v-list>
  23. </v-menu>
  24. </div>
  25. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. offset: true,
  11. }),
  12. }
  13. </script>

Menus(菜单) - 图9

Status Page

You can check the status of the documentation any time on our OhDear! uptime page.

ads by Vuetify

](https://status.vuetifyjs.com?ref=vuetifyjs.com)

Y 偏移

菜单可以通过 Y 轴偏移,以使激活器可见。

template script


  1. <template>
  2. <div class="text-center">
  3. <v-switch v-model="offset" label="Y offset"></v-switch>
  4. <v-menu top :offset-y="offset">
  5. <template v-slot:activator="{ on }">
  6. <v-btn
  7. color="primary"
  8. dark
  9. v-on="on"
  10. >
  11. Dropdown
  12. </v-btn>
  13. </template>
  14. <v-list>
  15. <v-list-item
  16. v-for="(item, index) in items"
  17. :key="index"
  18. @click=""
  19. >
  20. <v-list-item-title>{{ item.title }}</v-list-item-title>
  21. </v-list-item>
  22. </v-list>
  23. </v-menu>
  24. </div>
  25. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. offset: true,
  11. }),
  12. }
  13. </script>

Menus(菜单) - 图10

点击关闭

失去焦点时可以关闭菜单。

template script


  1. <template>
  2. <div class="text-center">
  3. <v-switch v-model="closeOnClick" label="Close on click"></v-switch>
  4. <v-menu top :close-on-click="closeOnClick">
  5. <template v-slot:activator="{ on }">
  6. <v-btn
  7. color="primary"
  8. dark
  9. v-on="on"
  10. >
  11. Dropdown
  12. </v-btn>
  13. </template>
  14. <v-list>
  15. <v-list-item
  16. v-for="(item, index) in items"
  17. :key="index"
  18. @click=""
  19. >
  20. <v-list-item-title>{{ item.title }}</v-list-item-title>
  21. </v-list-item>
  22. </v-list>
  23. </v-menu>
  24. </div>
  25. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. closeOnClick: true,
  11. }),
  12. }
  13. </script>

Menus(菜单) - 图11

点击内容后关闭

您可以配置单击 v-menu 的内容时是否应关闭它。

template script


  1. <template>
  2. <div class="text-center">
  3. <v-switch v-model="closeOnContentClick" label="Close on content click"></v-switch>
  4. <v-menu top :close-on-content-click="closeOnContentClick">
  5. <template v-slot:activator="{ on }">
  6. <v-btn
  7. color="primary"
  8. dark
  9. v-on="on"
  10. >
  11. Dropdown
  12. </v-btn>
  13. </template>
  14. <v-list>
  15. <v-list-item
  16. v-for="(item, index) in items"
  17. :key="index"
  18. @click=""
  19. >
  20. <v-list-item-title>{{ item.title }}</v-list-item-title>
  21. </v-list-item>
  22. </v-list>
  23. </v-menu>
  24. </div>
  25. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. closeOnContentClick: true,
  11. }),
  12. }
  13. </script>

Menus(菜单) - 图12

没有激活时的绝对定位

菜单也可以通过 absolute 属性跟 position-xposition-y 属性一起使用。尝试右键单击图像上的任意位置。

template script style


  1. <template>
  2. <div>
  3. <v-row class="flex" justify="center">
  4. <v-card
  5. :ripple="false"
  6. class="portrait"
  7. img="https://cdn.vuetifyjs.com/images/cards/girl.jpg"
  8. height="300px"
  9. @click="show"
  10. ></v-card>
  11. </v-row>
  12. <v-menu
  13. v-model="showMenu"
  14. :position-x="x"
  15. :position-y="y"
  16. absolute
  17. offset-y
  18. >
  19. <v-list>
  20. <v-list-item
  21. v-for="(item, index) in items"
  22. :key="index"
  23. @click=""
  24. >
  25. <v-list-item-title>{{ item.title }}</v-list-item-title>
  26. </v-list-item>
  27. </v-list>
  28. </v-menu>
  29. </div>
  30. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. showMenu: false,
  5. x: 0,
  6. y: 0,
  7. items: [
  8. { title: 'Click Me' },
  9. { title: 'Click Me' },
  10. { title: 'Click Me' },
  11. { title: 'Click Me 2' },
  12. ],
  13. }),
  14. methods: {
  15. show (e) {
  16. e.preventDefault()
  17. this.showMenu = false
  18. this.x = e.clientX
  19. this.y = e.clientY
  20. this.$nextTick(() => {
  21. this.showMenu = true
  22. })
  23. },
  24. },
  25. }
  26. </script>
  1. <style scoped>
  2. .portrait.v-card {
  3. margin: 0 auto;
  4. max-width: 600px;
  5. width: 100%;
  6. }
  7. </style>

Menus(菜单) - 图13

菜单

菜单可以放在几乎任何组件中

template script


  1. <template>
  2. <v-row>
  3. <v-col cols="12" sm="6" offset-sm="3">
  4. <v-card height="200px">
  5. <v-card-title class="blue white--text">
  6. <span class="headline">Menu</span>
  7. <v-spacer></v-spacer>
  8. <v-menu bottom left>
  9. <template v-slot:activator="{ on }">
  10. <v-btn
  11. dark
  12. icon
  13. v-on="on"
  14. >
  15. <v-icon>mdi-dots-vertical</v-icon>
  16. </v-btn>
  17. </template>
  18. <v-list>
  19. <v-list-item
  20. v-for="(item, i) in items"
  21. :key="i"
  22. @click=""
  23. >
  24. <v-list-item-title>{{ item.title }}</v-list-item-title>
  25. </v-list-item>
  26. </v-list>
  27. </v-menu>
  28. </v-card-title>
  29. <v-card-text>Lorem Ipsum</v-card-text>
  30. </v-card>
  31. </v-col>
  32. </v-row>
  33. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. { title: 'Click Me' },
  6. { title: 'Click Me' },
  7. { title: 'Click Me' },
  8. { title: 'Click Me 2' },
  9. ],
  10. }),
  11. }
  12. </script>

Menus(菜单) - 图14

弹出菜单

菜单可以配置为在打开时为静态菜单,使其充当弹出菜单。 当菜单内容中有多个交互式项目时,这很有用。

template script


  1. <template>
  2. <div class="text-center">
  3. <v-menu
  4. v-model="menu"
  5. :close-on-content-click="false"
  6. :nudge-width="200"
  7. offset-x
  8. >
  9. <template v-slot:activator="{ on }">
  10. <v-btn
  11. color="indigo"
  12. dark
  13. v-on="on"
  14. >
  15. Menu as Popover
  16. </v-btn>
  17. </template>
  18. <v-card>
  19. <v-list>
  20. <v-list-item>
  21. <v-list-item-avatar>
  22. <img src="https://cdn.vuetifyjs.com/images/john.jpg" alt="John">
  23. </v-list-item-avatar>
  24. <v-list-item-content>
  25. <v-list-item-title>John Leider</v-list-item-title>
  26. <v-list-item-subtitle>Founder of Vuetify.js</v-list-item-subtitle>
  27. </v-list-item-content>
  28. <v-list-item-action>
  29. <v-btn
  30. :class="fav ? 'red--text' : ''"
  31. icon
  32. @click="fav = !fav"
  33. >
  34. <v-icon>mdi-heart</v-icon>
  35. </v-btn>
  36. </v-list-item-action>
  37. </v-list-item>
  38. </v-list>
  39. <v-divider></v-divider>
  40. <v-list>
  41. <v-list-item>
  42. <v-list-item-action>
  43. <v-switch v-model="message" color="purple"></v-switch>
  44. </v-list-item-action>
  45. <v-list-item-title>Enable messages</v-list-item-title>
  46. </v-list-item>
  47. <v-list-item>
  48. <v-list-item-action>
  49. <v-switch v-model="hints" color="purple"></v-switch>
  50. </v-list-item-action>
  51. <v-list-item-title>Enable hints</v-list-item-title>
  52. </v-list-item>
  53. </v-list>
  54. <v-card-actions>
  55. <v-spacer></v-spacer>
  56. <v-btn text @click="menu = false">Cancel</v-btn>
  57. <v-btn color="primary" text @click="menu = false">Save</v-btn>
  58. </v-card-actions>
  59. </v-card>
  60. </v-menu>
  61. </div>
  62. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. fav: true,
  5. menu: false,
  6. message: false,
  7. hints: true,
  8. }),
  9. }
  10. </script>

Menus(菜单) - 图15

无障碍

默认情况下,v-menu 组件被 detached 并移动到你的应用程序的根目录下。为了正确地支持 将动态内容插入到 DOM,你必须_使用 attach prop。这将确保在按 tab 键时,焦点从激活器转移到内容上。