Drawer抽屉 - 图1

Drawer 抽屉

代码演示

Open

基本用法

基础抽屉,点击触发按钮抽屉从右滑出,点击遮罩区关闭

  1. <template>
  2. <a-button type="primary" @click="showDrawer">Open</a-button>
  3. <a-drawer
  4. title="Basic Drawer"
  5. placement="right"
  6. :closable="false"
  7. v-model:visible="visible"
  8. :after-visible-change="afterVisibleChange"
  9. >
  10. <p>Some contents...</p>
  11. <p>Some contents...</p>
  12. <p>Some contents...</p>
  13. </a-drawer>
  14. </template>
  15. <script lang="ts">
  16. import { defineComponent, ref } from 'vue';
  17. export default defineComponent({
  18. setup() {
  19. const visible = ref<boolean>(false);
  20. const afterVisibleChange = (bool: boolean) => {
  21. console.log('visible', bool);
  22. };
  23. const showDrawer = () => {
  24. visible.value = true;
  25. };
  26. return {
  27. visible,
  28. afterVisibleChange,
  29. showDrawer,
  30. };
  31. },
  32. });
  33. </script>

Drawer抽屉 - 图2

渲染在当前 DOM

渲染在当前 dom 里。自定义容器,查看 getContainer。

  1. <template>
  2. <div
  3. :style="{
  4. height: '200px',
  5. overflow: 'hidden',
  6. position: 'relative',
  7. border: '1px solid #ebedf0',
  8. borderRadius: '2px',
  9. padding: '48px',
  10. textAlign: 'center',
  11. background: '#fafafa',
  12. }"
  13. >
  14. Render in this
  15. <div style="margin-top: 16px">
  16. <a-button type="primary" @click="showDrawer">Open</a-button>
  17. </div>
  18. <a-drawer
  19. title="Basic Drawer"
  20. placement="right"
  21. :closable="false"
  22. :visible="visible"
  23. :get-container="false"
  24. :wrap-style="{ position: 'absolute' }"
  25. @close="onClose"
  26. >
  27. <p>Some contents...</p>
  28. </a-drawer>
  29. </div>
  30. </template>
  31. <script lang="ts">
  32. import { defineComponent, ref } from 'vue';
  33. export default defineComponent({
  34. setup() {
  35. const visible = ref(false);
  36. const afterVisibleChange = (bool: boolean) => {
  37. console.log('visible', bool);
  38. };
  39. const showDrawer = () => {
  40. visible.value = true;
  41. };
  42. const onClose = () => {
  43. visible.value = false;
  44. };
  45. return {
  46. visible,
  47. afterVisibleChange,
  48. showDrawer,
  49. onClose,
  50. };
  51. },
  52. });
  53. </script>

Drawer抽屉 - 图3

信息预览抽屉

需要快速预览对象概要时使用,点击遮罩区关闭。

  1. <template>
  2. <a-list
  3. :data-source="[
  4. {
  5. name: 'Lily',
  6. },
  7. {
  8. name: 'Lily',
  9. },
  10. ]"
  11. bordered
  12. >
  13. <template #renderItem="{ item }">
  14. <a-list-item :key="`a-${item.id}`">
  15. <template #actions><a @click="showDrawer">View Profile</a></template>
  16. <a-list-item-meta description="Progresser XTech">
  17. <template #title>
  18. <a href="https://www.antdv.com/">{{ item.name }}</a>
  19. </template>
  20. <template #avatar>
  21. <a-avatar src="https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png" />
  22. </template>
  23. </a-list-item-meta>
  24. </a-list-item>
  25. </template>
  26. </a-list>
  27. <a-drawer width="640" placement="right" :closable="false" :visible="visible" @close="onClose">
  28. <p :style="[pStyle, pStyle2]">User Profile</p>
  29. <p :style="pStyle">Personal</p>
  30. <a-row>
  31. <a-col :span="12">
  32. <description-item title="Full Name" content="Lily" />
  33. </a-col>
  34. <a-col :span="12">
  35. <description-item title="Account" content="AntDesign@example.com" />
  36. </a-col>
  37. </a-row>
  38. <a-row>
  39. <a-col :span="12">
  40. <description-item title="City" content="HangZhou" />
  41. </a-col>
  42. <a-col :span="12">
  43. <description-item title="Country" content="China🇨🇳" />
  44. </a-col>
  45. </a-row>
  46. <a-row>
  47. <a-col :span="12">
  48. <description-item title="Birthday" content="February 2,1900" />
  49. </a-col>
  50. <a-col :span="12">
  51. <description-item title="Website" content="-" />
  52. </a-col>
  53. </a-row>
  54. <a-row>
  55. <a-col :span="12">
  56. <description-item
  57. title="Message"
  58. content="Make things as simple as possible but no simpler."
  59. />
  60. </a-col>
  61. </a-row>
  62. <a-divider />
  63. <p :style="pStyle">Company</p>
  64. <a-row>
  65. <a-col :span="12">
  66. <description-item title="Position" content="Programmer" />
  67. </a-col>
  68. <a-col :span="12">
  69. <description-item title="Responsibilities" content="Coding" />
  70. </a-col>
  71. </a-row>
  72. <a-row>
  73. <a-col :span="12">
  74. <description-item title="Department" content="XTech" />
  75. </a-col>
  76. <a-col :span="12">
  77. <description-item title="Supervisor">
  78. <template #content><a>Lin</a></template>
  79. </description-item>
  80. </a-col>
  81. </a-row>
  82. <a-row>
  83. <a-col :span="24">
  84. <description-item
  85. title="Skills"
  86. content="C / C + +, data structures, software engineering, operating systems, computer networks, databases, compiler theory, computer architecture, Microcomputer Principle and Interface Technology, Computer English, Java, ASP, etc."
  87. />
  88. </a-col>
  89. </a-row>
  90. <a-divider />
  91. <p :style="pStyle">Contacts</p>
  92. <a-row>
  93. <a-col :span="12">
  94. <description-item title="Email" content="ant-design-vue@example.com" />
  95. </a-col>
  96. <a-col :span="12">
  97. <description-item title="Phone Number" content="+86 181 0000 0000" />
  98. </a-col>
  99. </a-row>
  100. <a-row>
  101. <a-col :span="24">
  102. <description-item title="Github">
  103. <template #content>
  104. <a href="https://github.com/vueComponent/ant-design-vue">
  105. github.com/vueComponent/ant-design-vue
  106. </a>
  107. </template>
  108. </description-item>
  109. </a-col>
  110. </a-row>
  111. </a-drawer>
  112. </template>
  113. <script lang="ts">
  114. import descriptionItem from './descriptionItem/index.vue';
  115. import { defineComponent, ref } from 'vue';
  116. export default defineComponent({
  117. components: {
  118. descriptionItem,
  119. },
  120. setup() {
  121. const visible = ref<boolean>(false);
  122. const pStyle = {
  123. fontSize: '16px',
  124. color: 'rgba(0,0,0,0.85)',
  125. lineHeight: '24px',
  126. display: 'block',
  127. marginBottom: '16px',
  128. };
  129. const pStyle2 = {
  130. marginBottom: '24px',
  131. };
  132. const showDrawer = () => {
  133. visible.value = true;
  134. };
  135. const onClose = () => {
  136. visible.value = false;
  137. };
  138. return {
  139. visible,
  140. pStyle,
  141. pStyle2,
  142. showDrawer,
  143. onClose,
  144. };
  145. },
  146. });
  147. </script>

Drawer抽屉 - 图4

Open

自定义位置

自定义位置,点击触发按钮抽屉从相应的位置滑出,点击遮罩区关闭

  1. <template>
  2. <a-radio-group style="margin-right: 8px" v-model:value="placement">
  3. <a-radio value="top">top</a-radio>
  4. <a-radio value="right">right</a-radio>
  5. <a-radio value="bottom">bottom</a-radio>
  6. <a-radio value="left">left</a-radio>
  7. </a-radio-group>
  8. <a-button type="primary" @click="showDrawer">Open</a-button>
  9. <a-drawer
  10. title="Basic Drawer"
  11. :placement="placement"
  12. :closable="false"
  13. :visible="visible"
  14. @close="onClose"
  15. >
  16. <p>Some contents...</p>
  17. <p>Some contents...</p>
  18. <p>Some contents...</p>
  19. </a-drawer>
  20. </template>
  21. <script lang="ts">
  22. import { defineComponent, ref } from 'vue';
  23. export default defineComponent({
  24. setup() {
  25. const placement = ref<string>('left');
  26. const visible = ref<boolean>(false);
  27. const showDrawer = () => {
  28. visible.value = true;
  29. };
  30. const onClose = () => {
  31. visible.value = false;
  32. };
  33. return {
  34. placement,
  35. visible,
  36. showDrawer,
  37. onClose,
  38. };
  39. },
  40. });
  41. </script>

Drawer抽屉 - 图5New account

抽屉表单

在抽屉中使用表单。

  1. <template>
  2. <a-button type="primary" @click="showDrawer">
  3. <PlusOutlined />
  4. New account
  5. </a-button>
  6. <a-drawer
  7. title="Create a new account"
  8. :width="720"
  9. :visible="visible"
  10. :body-style="{ paddingBottom: '80px' }"
  11. @close="onClose"
  12. >
  13. <a-form :model="form" :rules="rules" layout="vertical">
  14. <a-row :gutter="16">
  15. <a-col :span="12">
  16. <a-form-item label="Name" name="name">
  17. <a-input v-model:value="form.name" placeholder="Please enter user name" />
  18. </a-form-item>
  19. </a-col>
  20. <a-col :span="12">
  21. <a-form-item label="Url" name="url">
  22. <a-input
  23. v-model:value="form.url"
  24. style="width: 100%"
  25. addon-before="http://"
  26. addon-after=".com"
  27. placeholder="please enter url"
  28. />
  29. </a-form-item>
  30. </a-col>
  31. </a-row>
  32. <a-row :gutter="16">
  33. <a-col :span="12">
  34. <a-form-item label="Owner" name="owner">
  35. <a-select placeholder="Please a-s an owner" v-model:value="form.owner">
  36. <a-select-option value="xiao">Xiaoxiao Fu</a-select-option>
  37. <a-select-option value="mao">Maomao Zhou</a-select-option>
  38. </a-select>
  39. </a-form-item>
  40. </a-col>
  41. <a-col :span="12">
  42. <a-form-item label="Type" name="type">
  43. <a-select placeholder="Please choose the type" v-model:value="form.type">
  44. <a-select-option value="private">Private</a-select-option>
  45. <a-select-option value="public">Public</a-select-option>
  46. </a-select>
  47. </a-form-item>
  48. </a-col>
  49. </a-row>
  50. <a-row :gutter="16">
  51. <a-col :span="12">
  52. <a-form-item label="Approver" name="approver">
  53. <a-select placeholder="Please choose the approver" v-model:value="form.approver">
  54. <a-select-option value="jack">Jack Ma</a-select-option>
  55. <a-select-option value="tom">Tom Liu</a-select-option>
  56. </a-select>
  57. </a-form-item>
  58. </a-col>
  59. <a-col :span="12">
  60. <a-form-item label="DateTime" name="dateTime">
  61. <a-date-picker
  62. v-model:value="form.dateTime"
  63. style="width: 100%"
  64. :get-popup-container="trigger => trigger.parentNode"
  65. />
  66. </a-form-item>
  67. </a-col>
  68. </a-row>
  69. <a-row :gutter="16">
  70. <a-col :span="24">
  71. <a-form-item label="Description" name="description">
  72. <a-textarea
  73. v-model:value="form.description"
  74. :rows="4"
  75. placeholder="please enter url description"
  76. />
  77. </a-form-item>
  78. </a-col>
  79. </a-row>
  80. </a-form>
  81. <div
  82. :style="{
  83. position: 'absolute',
  84. right: 0,
  85. bottom: 0,
  86. width: '100%',
  87. borderTop: '1px solid #e9e9e9',
  88. padding: '10px 16px',
  89. background: '#fff',
  90. textAlign: 'right',
  91. zIndex: 1,
  92. }"
  93. >
  94. <a-button style="margin-right: 8px" @click="onClose">Cancel</a-button>
  95. <a-button type="primary" @click="onClose">Submit</a-button>
  96. </div>
  97. </a-drawer>
  98. </template>
  99. <script lang="ts">
  100. import { PlusOutlined } from '@ant-design/icons-vue';
  101. import { defineComponent, reactive, ref } from 'vue';
  102. export default defineComponent({
  103. components: {
  104. PlusOutlined,
  105. },
  106. setup() {
  107. const form = reactive({
  108. name: '',
  109. url: '',
  110. owner: '',
  111. type: '',
  112. approver: '',
  113. dateTime: '',
  114. description: '',
  115. });
  116. const rules = {
  117. name: [{ required: true, message: 'Please enter user name' }],
  118. url: [{ required: true, message: 'please enter url' }],
  119. owner: [{ required: true, message: 'Please select an owner' }],
  120. type: [{ required: true, message: 'Please choose the type' }],
  121. approver: [{ required: true, message: 'Please choose the approver' }],
  122. dateTime: [{ required: true, message: 'Please choose the dateTime', type: 'object' }],
  123. description: [{ required: true, message: 'Please enter url description' }],
  124. };
  125. const visible = ref<boolean>(false);
  126. const showDrawer = () => {
  127. visible.value = true;
  128. };
  129. const onClose = () => {
  130. visible.value = false;
  131. };
  132. return {
  133. form,
  134. rules,
  135. visible,
  136. showDrawer,
  137. onClose,
  138. };
  139. },
  140. });
  141. </script>

Open

多层抽屉

在抽屉内打开新的抽屉,用以解决多分支任务的复杂状况。

  1. <template>
  2. <a-button type="primary" @click="showDrawer">Open</a-button>
  3. <a-drawer
  4. title="Multi-level drawer"
  5. width="520"
  6. :closable="false"
  7. :visible="visible"
  8. @close="onClose"
  9. >
  10. <a-button type="primary" @click="showChildrenDrawer">Two-level drawer</a-button>
  11. <a-drawer
  12. title="Two-level Drawer"
  13. width="320"
  14. :closable="false"
  15. :visible="childrenDrawer"
  16. @close="onChildrenDrawerClose"
  17. >
  18. <a-button type="primary" @click="showChildrenDrawer">This is two-level drawer</a-button>
  19. </a-drawer>
  20. <div
  21. :style="{
  22. position: 'absolute',
  23. bottom: 0,
  24. width: '100%',
  25. borderTop: '1px solid #e8e8e8',
  26. padding: '10px 16px',
  27. textAlign: 'right',
  28. left: 0,
  29. background: '#fff',
  30. borderRadius: '0 0 4px 4px',
  31. }"
  32. >
  33. <a-button style="margin-right: 8px" @click="onClose">Cancel</a-button>
  34. <a-button type="primary" @click="onClose">Submit</a-button>
  35. </div>
  36. </a-drawer>
  37. </template>
  38. <script lang="ts">
  39. import { defineComponent, ref } from 'vue';
  40. export default defineComponent({
  41. setup() {
  42. const visible = ref<boolean>(false);
  43. const childrenDrawer = ref<boolean>(false);
  44. const showDrawer = () => {
  45. visible.value = true;
  46. };
  47. const onClose = () => {
  48. visible.value = false;
  49. };
  50. const showChildrenDrawer = () => {
  51. childrenDrawer.value = true;
  52. };
  53. const onChildrenDrawerClose = () => {
  54. childrenDrawer.value = false;
  55. };
  56. return {
  57. visible,
  58. childrenDrawer,
  59. showDrawer,
  60. onClose,
  61. showChildrenDrawer,
  62. onChildrenDrawerClose,
  63. };
  64. },
  65. });
  66. </script>

API

参数说明类型默认值版本
closable是否显示右上角的关闭按钮booleantrue
destroyOnClose关闭时销毁 Drawer 里的子元素booleanfalse
getContainer指定 Drawer 挂载的 HTML 节点HTMLElement | () => HTMLElement | Selectors‘body’
maskClosable点击蒙层是否允许关闭booleantrue
mask是否展示遮罩Booleantrue
maskStyle遮罩样式object{}
title标题string | slot-
visible(v-model)Drawer 是否可见boolean-
wrapClassName对话框外层容器的类名string-
wrapStyle可用于设置 Drawer 最外层容器的样式,和 drawerStyle 的区别是作用节点包括 maskobject-
drawerStyle用于设置 Drawer 弹出层的样式object-
headerStyle用于设置 Drawer 头部的样式object-
bodyStyle可用于设置 Drawer 内容部分的样式object-
width宽度string | number256
height高度, 在 placementtopbottom 时使用string | number256
zIndex设置 Drawer 的 z-indexNumber1000
placement抽屉的方向‘top’ | ‘right’ | ‘bottom’ | ‘left’‘right’
handle设置后抽屉直接挂载到 DOM 上,你可以通过该 handle 控制抽屉打开关闭VNode | slot-
afterVisibleChange切换抽屉时动画结束后的回调function(visible)
keyboard是否支持键盘 esc 关闭booleantrue

方法

名称描述类型默认值版本
close点击遮罩层或右上角叉或取消按钮的回调function(e)