可以左右滑動來展示操作按鈕的單元格組件。
通過以下方式來全局注冊組件,更多注冊方式請參考組件注冊。
import { createApp } from 'vue';
import { SwipeCell } from 'vant';
const app = createApp();
app.use(SwipeCell);
?SwipeCell
? 組件提供了 ?left
? 和 ?right
? 兩個插槽,用于定義兩側滑動區(qū)域的內容。
<van-swipe-cell>
<template #left>
<van-button square type="primary" text="選擇" />
</template>
<van-cell :border="false" title="單元格" value="內容" />
<template #right>
<van-button square type="danger" text="刪除" />
<van-button square type="primary" text="收藏" />
</template>
</van-swipe-cell>
?SwipeCell
? 可以嵌套任意內容,比如嵌套一個商品卡片。
<van-swipe-cell>
<van-card
num="2"
price="2.00"
desc="描述信息"
title="商品標題"
class="goods-card"
thumb="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg"
/>
<template #right>
<van-button square text="刪除" type="danger" class="delete-button" />
</template>
</van-swipe-cell>
<style>
.goods-card {
margin: 0;
background-color: @white;
}
.delete-button {
height: 100%;
}
</style>
通過傳入 ?before-close
? 回調函數,可以自定義兩側滑動內容關閉時的行為。
<van-swipe-cell :before-close="beforeClose">
<template #left>
<van-button square type="primary" text="選擇" />
</template>
<van-cell :border="false" title="單元格" value="內容" />
<template #right>
<van-button square type="danger" text="刪除" />
</template>
</van-swipe-cell>
import { showConfirmDialog } from 'vant';
export default {
setup() {
// position 為關閉時點擊的位置
const beforeClose = ({ position }) => {
switch (position) {
case 'left':
case 'cell':
case 'outside':
return true;
case 'right':
return new Promise((resolve) => {
showConfirmDialog({
title: '確定刪除嗎?',
}).then(resolve);
});
}
};
return { beforeClose };
},
};
參數 | 說明 | 類型 | 默認值 |
---|---|---|---|
name | 標識符,通常為一個唯一的字符串或數字,可以在事件參數中獲取到 | number | string | ''
|
left-width | 指定左側滑動區(qū)域寬度,單位為 px
|
number | string | auto
|
right-width | 指定右側滑動區(qū)域寬度,單位為 px
|
number | string | auto
|
before-close | 關閉前的回調函數,返回 false 可阻止關閉,支持返回 Promise |
(args) => boolean | Promise<boolean> | - |
disabled | 是否禁用滑動 | boolean | false
|
stop-propagation | 是否阻止滑動事件冒泡 | boolean | false
|
名稱 | 說明 |
---|---|
default | 默認顯示的內容 |
left | 左側滑動區(qū)域的內容 |
right | 右側滑動區(qū)域的內容 |
事件名 | 說明 | 回調參數 |
---|---|---|
click | 點擊時觸發(fā) | position: 'left' | 'right' | 'cell' | 'outside' |
open | 打開時觸發(fā) | { name: string | number, position: 'left' | 'right' } |
close | 關閉時觸發(fā) | { name: string | number, position: 'left' | 'right' | 'cell' | 'outside' } |
beforeClose 的第一個參數為對象,對象中包含以下屬性:
參數名 | 說明 | 類型 |
---|---|---|
name | 標識符 | string | number |
position | 關閉時的點擊位置 | 'left' | 'right' | 'cell' | 'outside' |
通過 ref 可以獲取到 SwipeCell 實例并調用實例方法,詳見組件實例方法。
方法名 | 說明 | 參數 | 返回值 |
---|---|---|---|
open | 打開單元格側邊欄 | position: left | right
|
- |
close | 收起單元格側邊欄 | - | - |
組件導出以下類型定義:
import type {
SwipeCellSide,
SwipeCellProps,
SwipeCellPosition,
SwipeCellInstance,
} from 'vant';
SwipeCellInstance 是組件實例的類型,用法如下:
import { ref } from 'vue';
import type { SwipeCellInstance } from 'vant';
const swipeCellRef = ref<SwipeCellInstance>();
swipeCellRef.value?.close();
參見桌面端適配。
更多建議: