Popconfirm气泡确认框 - 图1

Popconfirm 气泡确认框

点击元素,弹出气泡式的确认框。

何时使用

目标元素的操作需要用户进一步的确认时,在目标元素附近弹出浮层提示,询问用户。 和 ‘confirm’ 弹出的全屏居中模态对话框相比,交互形式更轻量。

代码演示

Delete

基本用法

最简单的用法。

  1. <template>
  2. <a-popconfirm
  3. title="Are you sure delete this task?"
  4. ok-text="Yes"
  5. cancel-text="No"
  6. @confirm="confirm"
  7. @cancel="cancel"
  8. >
  9. <a href="#">Delete</a>
  10. </a-popconfirm>
  11. </template>
  12. <script lang="ts">
  13. import { defineComponent } from 'vue';
  14. import { message } from 'ant-design-vue';
  15. export default defineComponent({
  16. setup() {
  17. const confirm = (e: MouseEvent) => {
  18. console.log(e);
  19. message.success('Click on Yes');
  20. };
  21. const cancel = (e: MouseEvent) => {
  22. console.log(e);
  23. message.error('Click on No');
  24. };
  25. return {
  26. confirm,
  27. cancel,
  28. };
  29. },
  30. });
  31. </script>

Delete

自定义 Icon 图标

使用 icon 自定义提示 icon

  1. <template>
  2. <a-popconfirm title="Are you sure?">
  3. <template #icon><question-circle-outlined style="color: red" /></template>
  4. <a href="#">Delete</a>
  5. </a-popconfirm>
  6. </template>
  7. <script lang="ts">
  8. import { QuestionCircleOutlined } from '@ant-design/icons-vue';
  9. import { defineComponent } from 'vue';
  10. export default defineComponent({
  11. components: {
  12. QuestionCircleOutlined,
  13. },
  14. });
  15. </script>

Popconfirm气泡确认框 - 图2

条件触发

可以判断是否需要弹出。

  1. <template>
  2. <div>
  3. <a-popconfirm
  4. title="Are you sure delete this task?"
  5. :visible="visible"
  6. ok-text="Yes"
  7. cancel-text="No"
  8. @visibleChange="handleVisibleChange"
  9. @confirm="confirm"
  10. @cancel="cancel"
  11. >
  12. <a href="#">Delete a task</a>
  13. </a-popconfirm>
  14. <br />
  15. <br />
  16. Whether directly execute:
  17. <a-checkbox v-model:checked="condition" />
  18. </div>
  19. </template>
  20. <script lang="ts">
  21. import { message } from 'ant-design-vue';
  22. import { ref, defineComponent } from 'vue';
  23. export default defineComponent({
  24. setup() {
  25. const visible = ref<boolean>(false);
  26. const condition = ref<boolean>(true);
  27. const confirm = () => {
  28. visible.value = false;
  29. message.success('Next step.');
  30. };
  31. const cancel = () => {
  32. visible.value = false;
  33. message.error('Click on cancel.');
  34. };
  35. const handleVisibleChange = (bool: boolean) => {
  36. if (!bool) {
  37. visible.value = false;
  38. return;
  39. }
  40. // Determining condition before show the popconfirm.
  41. console.log(condition.value);
  42. if (condition.value) {
  43. confirm(); // next step
  44. } else {
  45. visible.value = true;
  46. }
  47. };
  48. return {
  49. visible,
  50. condition,
  51. confirm,
  52. cancel,
  53. handleVisibleChange,
  54. };
  55. },
  56. });
  57. </script>

Delete

国际化

使用 okTextcancelText 自定义按钮文字。

  1. <template>
  2. <a-popconfirm title="Are you sure?" ok-text="Yes" cancel-text="No">
  3. <a href="#">Delete</a>
  4. </a-popconfirm>
  5. </template>

Popconfirm气泡确认框 - 图3

位置

位置有十二个方向。如需箭头指向目标元素中心,可以设置 arrowPointAtCenter

  1. <template>
  2. <div id="components-a-popconfirm-demo-placement">
  3. <div :style="{ marginLeft: `${buttonWidth}px`, whiteSpace: 'nowrap' }">
  4. <a-popconfirm placement="topLeft" ok-text="Yes" cancel-text="No" @confirm="confirm">
  5. <template #title>
  6. <p>{{ text }}</p>
  7. <p>{{ text }}</p>
  8. </template>
  9. <a-button>TL</a-button>
  10. </a-popconfirm>
  11. <a-popconfirm placement="top" ok-text="Yes" cancel-text="No" @confirm="confirm">
  12. <template #title>
  13. <p>{{ text }}</p>
  14. <p>{{ text }}</p>
  15. </template>
  16. <a-button>Top</a-button>
  17. </a-popconfirm>
  18. <a-popconfirm placement="topRight" ok-text="Yes" cancel-text="No" @confirm="confirm">
  19. <template #title>
  20. <p>{{ text }}</p>
  21. <p>{{ text }}</p>
  22. </template>
  23. <a-button>TR</a-button>
  24. </a-popconfirm>
  25. </div>
  26. <div :style="{ width: `${buttonWidth}px`, float: 'left' }">
  27. <a-popconfirm placement="leftTop" ok-text="Yes" cancel-text="No" @confirm="confirm">
  28. <template #title>
  29. <p>{{ text }}</p>
  30. <p>{{ text }}</p>
  31. </template>
  32. <a-button>LT</a-button>
  33. </a-popconfirm>
  34. <a-popconfirm placement="left" ok-text="Yes" cancel-text="No" @confirm="confirm">
  35. <template #title>
  36. <p>{{ text }}</p>
  37. <p>{{ text }}</p>
  38. </template>
  39. <a-button>Left</a-button>
  40. </a-popconfirm>
  41. <a-popconfirm placement="leftBottom" ok-text="Yes" cancel-text="No" @confirm="confirm">
  42. <template #title>
  43. <p>{{ text }}</p>
  44. <p>{{ text }}</p>
  45. </template>
  46. <a-button>LB</a-button>
  47. </a-popconfirm>
  48. </div>
  49. <div :style="{ width: `${buttonWidth}px`, marginLeft: `${buttonWidth * 4 + 24}px` }">
  50. <a-popconfirm placement="rightTop" ok-text="Yes" cancel-text="No" @confirm="confirm">
  51. <template #title>
  52. <p>{{ text }}</p>
  53. <p>{{ text }}</p>
  54. </template>
  55. <a-button>RT</a-button>
  56. </a-popconfirm>
  57. <a-popconfirm placement="right" ok-text="Yes" cancel-text="No" @confirm="confirm">
  58. <template #title>
  59. <p>{{ text }}</p>
  60. <p>{{ text }}</p>
  61. </template>
  62. <a-button>Right</a-button>
  63. </a-popconfirm>
  64. <a-popconfirm placement="rightBottom" ok-text="Yes" cancel-text="No" @confirm="confirm">
  65. <template #title>
  66. <p>{{ text }}</p>
  67. <p>{{ text }}</p>
  68. </template>
  69. <a-button>RB</a-button>
  70. </a-popconfirm>
  71. </div>
  72. <div :style="{ marginLeft: `${buttonWidth}px`, clear: 'both', whiteSpace: 'nowrap' }">
  73. <a-popconfirm placement="bottomLeft" ok-text="Yes" cancel-text="No" @confirm="confirm">
  74. <template #title>
  75. <p>{{ text }}</p>
  76. <p>{{ text }}</p>
  77. </template>
  78. <a-button>BL</a-button>
  79. </a-popconfirm>
  80. <a-popconfirm placement="bottom" ok-text="Yes" cancel-text="No" @confirm="confirm">
  81. <template #title>
  82. <p>{{ text }}</p>
  83. <p>{{ text }}</p>
  84. </template>
  85. <a-button>Bottom</a-button>
  86. </a-popconfirm>
  87. <a-popconfirm placement="bottomRight" ok-text="Yes" cancel-text="No" @confirm="confirm">
  88. <template #title>
  89. <p>{{ text }}</p>
  90. <p>{{ text }}</p>
  91. </template>
  92. <a-button>BR</a-button>
  93. </a-popconfirm>
  94. </div>
  95. </div>
  96. </template>
  97. <script>
  98. import { message } from 'ant-design-vue';
  99. import { defineComponent } from 'vue';
  100. export default defineComponent({
  101. setup() {
  102. const buttonWidth = 70;
  103. const text = 'Are you sure to delete this task?';
  104. const confirm = () => {
  105. message.info('Clicked on Yes.');
  106. };
  107. return {
  108. buttonWidth,
  109. text,
  110. confirm,
  111. };
  112. },
  113. });
  114. </script>
  115. <style scoped>
  116. #components-a-popconfirm-demo-placement .ant-btn {
  117. width: 70px;
  118. text-align: center;
  119. padding: 0;
  120. margin-right: 8px;
  121. margin-bottom: 8px;
  122. }
  123. </style>

API

参数说明类型默认值版本
cancelText取消按钮文字string|slot取消
okText确认按钮文字string|slot确定
okType确认按钮类型stringprimary
title确认框的描述string|slot
icon自定义弹出气泡 Icon 图标vNode<Icon type=”exclamation-circle” />
disabled点击 Popconfirm 子元素是否弹出气泡确认框booleanfalse

事件

事件名称说明回调参数
cancel点击取消的回调function(e)
confirm点击确认的回调function(e)
visibleChange显示隐藏的回调function(visible)

更多属性请参考 Tooltip

注意

请确保 Popconfirm 的子元素能接受 mouseentermouseleavefocusclick 事件。