Popover 气泡弹出框

引入

通过以下方式来全局注册组件,更多注册方式请参考组件注册

基础用法

当 Popover 弹出时,会基于 插槽的内容进行定位。

  1. <van-popover v-model:show="showPopover" :actions="actions" @select="onSelect">
  2. <template #reference>
  3. <van-button type="primary">浅色风格</van-button>
  4. </template>
  5. </van-popover>
  1. import { ref } from 'vue';
  2. import { showToast } from 'vant';
  3. export default {
  4. setup() {
  5. const showPopover = ref(false);
  6. // 通过 actions 属性来定义菜单选项
  7. const actions = [
  8. { text: '选项一' },
  9. { text: '选项二' },
  10. { text: '选项三' },
  11. ];
  12. const onSelect = (action) => showToast(action.text);
  13. return {
  14. actions,
  15. onSelect,
  16. showPopover,
  17. };
  18. },
  19. };

深色风格

Popover 支持浅色和深色两种风格,默认为浅色风格,将 theme 属性设置为 dark 可切换为深色风格。

  1. <van-popover v-model:show="showPopover" theme="dark" :actions="actions">
  2. <template #reference>
  3. <van-button type="primary">深色风格</van-button>
  4. </template>
  5. </van-popover>
  1. export default {
  2. setup() {
  3. const showPopover = ref(false);
  4. const actions = [
  5. { text: '选项一' },
  6. { text: '选项二' },
  7. { text: '选项三' },
  8. ];
  9. return {
  10. actions,
  11. showPopover,
  12. };
  13. },
  14. };

弹出位置

通过 placement 属性来控制气泡的弹出位置。

  1. top # 顶部中间位置
  2. top-start # 顶部左侧位置
  3. top-end # 顶部右侧位置
  4. left # 左侧中间位置
  5. left-start # 左侧上方位置
  6. right # 右侧中间位置
  7. right-start # 右侧上方位置
  8. right-end # 右侧下方位置
  9. bottom # 底部中间位置
  10. bottom-start # 底部左侧位置
  11. bottom-end # 底部右侧位置

actions 数组中,可以通过 icon 字段来定义选项的图标,支持传入图标名称或图片链接,等同于 Icon 组件的 name 属性

  1. <van-popover v-model:show="showPopover" :actions="actions">
  2. <template #reference>
  3. <van-button type="primary">展示图标</van-button>
  4. </template>
  5. </van-popover>
  1. import { ref } from 'vue';
  2. export default {
  3. setup() {
  4. const showPopover = ref(false);
  5. const actions = [
  6. { text: '选项一', icon: 'add-o' },
  7. { text: '选项二', icon: 'music-o' },
  8. { text: '选项三', icon: 'more-o' },
  9. ];
  10. return {
  11. actions,
  12. showPopover,
  13. };
  14. },
  15. };

禁用选项

actions 数组中,可以通过 disabled 字段来禁用某个选项。

  1. <van-popover v-model:show="showPopover" :actions="actions">
  2. <template #reference>
  3. </template>
  4. </van-popover>

自定义内容

通过默认插槽,可以在 Popover 内部放置任意内容。

  1. <van-popover v-model:show="showPopover">
  2. <van-grid
  3. square
  4. clickable
  5. :border="false"
  6. column-num="3"
  7. style="width: 240px;"
  8. >
  9. <van-grid-item
  10. v-for="i in 6"
  11. :key="i"
  12. text="选项"
  13. icon="photo-o"
  14. @click="showPopover = false"
  15. />
  16. </van-grid>
  17. <template #reference>
  18. <van-button type="primary">自定义内容</van-button>
  19. </template>
  20. </van-popover>
  1. import { ref } from 'vue';
  2. export default {
  3. setup() {
  4. const showPopover = ref(false);
  5. return { showPopover };
  6. },
  7. };

非受控模式

你可以把 Popover 当做受控组件或非受控组件使用:

  • 当绑定 v-model:show 时,Popover 为受控组件,此时组件的显示完全由 v-model:show 的值决定。
  • 当未绑定 v-model:show 时,Popover 为非受控组件,此时你可以通过 show 属性传入一个默认值,组件值的显示由组件自身控制。
  1. <van-popover :actions="actions" position="top-start" @select="onSelect">
  2. <template #reference>
  3. <van-button type="primary">非受控模式</van-button>
  4. </template>
  5. </van-popover>
  1. import { ref } from 'vue';
  2. import { showToast } from 'vant';
  3. export default {
  4. setup() {
  5. const actions = [
  6. { text: '选项一' },
  7. { text: '选项二' },
  8. { text: '选项三' },
  9. ];
  10. const onSelect = (action) => showToast(action.text);
  11. return {
  12. actions,
  13. onSelect,
  14. };
  15. };

Props

Events

Slots

类型定义

组件导出以下类型定义:

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

这种情况通常是由于项目中引入了 fastclick 库导致的。建议移除 fastclick,或者配置 fastclick 的 。