SwipeCell 滑动单元格

介绍

可以左右滑动来展示操作按钮的单元格组件。

引入

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

  1. import { createApp } from 'vue';
  2. import { SwipeCell } from 'vant';
  3. const app = createApp();
  4. app.use(SwipeCell);

代码演示

基础用法

SwipeCell 组件提供了 leftright 两个插槽,用于定义两侧滑动区域的内容。

  1. <van-swipe-cell>
  2. <template #left>
  3. <van-button square type="primary" text="选择" />
  4. </template>
  5. <van-cell :border="false" title="单元格" value="内容" />
  6. <template #right>
  7. <van-button square type="danger" text="删除" />
  8. <van-button square type="primary" text="收藏" />
  9. </template>
  10. </van-swipe-cell>

自定义内容

SwipeCell 可以嵌套任意内容,比如嵌套一个商品卡片。

  1. <van-swipe-cell>
  2. <van-card
  3. num="2"
  4. price="2.00"
  5. desc="描述信息"
  6. title="商品标题"
  7. class="goods-card"
  8. thumb="https://img.yzcdn.cn/vant/cat.jpeg"
  9. />
  10. <template #right>
  11. <van-button square text="删除" type="danger" class="delete-button" />
  12. </template>
  13. </van-swipe-cell>
  14. <style>
  15. .goods-card {
  16. margin: 0;
  17. background-color: @white;
  18. }
  19. .delete-button {
  20. height: 100%;
  21. }
  22. </style>

异步关闭

通过传入 before-close 回调函数,可以自定义两侧滑动内容关闭时的行为。

  1. <van-swipe-cell :before-close="beforeClose">
  2. <template #left>
  3. <van-button square type="primary" text="选择" />
  4. </template>
  5. <van-cell :border="false" title="单元格" value="内容" />
  6. <template #right>
  7. <van-button square type="danger" text="删除" />
  8. </template>
  9. </van-swipe-cell>
  1. import { Dialog } from 'vant';
  2. export default {
  3. setup() {
  4. // position 为关闭时点击的位置
  5. const beforeClose = ({ position }) => {
  6. switch (position) {
  7. case 'left':
  8. case 'cell':
  9. case 'outside':
  10. return true;
  11. case 'right':
  12. return new Promise((resolve) => {
  13. Dialog.confirm({
  14. title: '确定删除吗?',
  15. }).then(resolve);
  16. });
  17. }
  18. };
  19. return { beforeClose };
  20. },
  21. };

API

Props

参数说明类型默认值
name标识符,可以在事件参数中获取到number | string‘’
left-width指定左侧滑动区域宽度,单位为 pxnumber | stringauto
right-width指定右侧滑动区域宽度,单位为 pxnumber | stringauto
before-close关闭前的回调函数,返回 false 可阻止关闭,支持返回 Promise(args) => boolean | Promise<boolean>-
disabled是否禁用滑动booleanfalse
stop-propagation是否阻止滑动事件冒泡booleanfalse

Slots

名称说明
default默认显示的内容
left左侧滑动区域的内容
right右侧滑动区域的内容

Events

事件名说明回调参数
click点击时触发position: ‘left’ | ‘right’ | ‘cell’ | ‘outside’
open打开时触发{ name: string | number, position: ‘left’ | ‘right’ }
close关闭时触发{ name: string | number, position: ‘left’ | ‘right’ | ‘cell’ | ‘outside’ }

beforeClose 参数

beforeClose 的第一个参数为对象,对象中包含以下属性:

参数名说明类型
name标识符string | number
position关闭时的点击位置‘left’ | ‘right’ | ‘cell’ | ‘outside’

方法

通过 ref 可以获取到 SwipeCell 实例并调用实例方法,详见组件实例方法

方法名说明参数返回值
open打开单元格侧边栏position: left | right-
close收起单元格侧边栏--

类型定义

组件导出以下类型定义:

  1. import type {
  2. SwipeCellSide,
  3. SwipeCellProps,
  4. SwipeCellPosition,
  5. SwipeCellInstance,
  6. } from 'vant';

SwipeCellInstance 是组件实例的类型,用法如下:

  1. import { ref } from 'vue';
  2. import type { SwipeCellInstance } from 'vant';
  3. const swipeCellRef = ref<SwipeCellInstance>();
  4. swipeCellRef.value?.close();

主题定制

样式变量

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

名称默认值描述
—van-switch-cell-padding-topvar(—van-cell-vertical-padding) - 1px-
—van-switch-cell-padding-bottomvar(—van-cell-vertical-padding) - 1px-
—van-switch-cell-large-padding-topvar(—van-cell-large-vertical-padding) - 1px-
—van-switch-cell-large-padding-bottomvar(—van-cell-large-vertical-padding) - 1px-

常见问题

在桌面端无法操作组件?

参见桌面端适配

SwipeCell 滑动单元格 - 图1