import Vue from 'vue';
import { SwipeCell } from 'vant';
Vue.use(SwipeCell);
SwipeCell組件提供了left和right兩個(gè)插槽,用于定義兩側(cè)滑動(dòng)區(qū)域的內(nèi)容
<van-swipe-cell>
<template slot="left">
<van-button square type="primary" text="選擇" />
</template>
<van-cell :border="false" title="單元格" value="內(nèi)容" />
<template slot="right">
<van-button square type="danger" text="刪除" />
<van-button square type="primary" text="收藏"/>
</template>
</van-swipe-cell>
SwipeCell內(nèi)容可以嵌套任意內(nèi)容,比如嵌套一個(gè)商品卡片
<van-swipe-cell>
<van-card
num="2"
price="2.00"
desc="描述信息"
title="商品標(biāo)題"
class="goods-card"
thumb="https://img.yzcdn.cn/vant/cat.jpeg"
/>
<van-button
slot="right"
square
text="刪除"
type="danger"
class="delete-button"
/>
</van-swipe-cell>
<style>
.goods-card {
margin: 0;
background-color: @white;
}
.delete-button {
height: 100%;
}
</style>
通過(guò)傳入before-close回調(diào)函數(shù),可以自定義兩側(cè)滑動(dòng)內(nèi)容關(guān)閉時(shí)的行為
<van-swipe-cell :before-close="beforeClose">
<template slot="left">
<van-button square type="primary" text="選擇" />
</template>
<van-cell :border="false" title="單元格" value="內(nèi)容" />
<template slot="right">
<van-button square type="danger" text="刪除" />
</template>
</van-swipe-cell>
export default {
methods: {
// position 為關(guān)閉時(shí)點(diǎn)擊的位置
// instance 為對(duì)應(yīng)的 SwipeCell 實(shí)例
beforeClose({ position, instance }) {
switch (position) {
case 'left':
case 'cell':
case 'outside':
instance.close();
break;
case 'right':
Dialog.confirm({
message: '確定刪除嗎?'
}).then(() => {
instance.close();
});
break;
}
}
}
}
參數(shù) | 說(shuō)明 | 類(lèi)型 | 默認(rèn)值 |
---|---|---|---|
name v2.0.4 | 標(biāo)識(shí)符,可以在事件參數(shù)中獲取到 | number | string | - |
left-width | 指定左側(cè)滑動(dòng)區(qū)域?qū)挾?,單位?code>px | number | string | auto |
right-width | 指定右側(cè)滑動(dòng)區(qū)域?qū)挾?,單位?code>px | number | string | auto |
before-close v2.3.0 | 關(guān)閉前的回調(diào)函數(shù) | Function | - |
disabled | 是否禁用滑動(dòng) | boolean | false |
stop-propagation v2.1.0 | 是否阻止滑動(dòng)事件冒泡 | boolean | false |
名稱(chēng) | 說(shuō)明 |
---|---|
default | 自定義顯示內(nèi)容 |
left | 左側(cè)滑動(dòng)內(nèi)容 |
right | 右側(cè)滑動(dòng)內(nèi)容 |
事件名 | 說(shuō)明 | 回調(diào)參數(shù) |
---|---|---|
click | 點(diǎn)擊時(shí)觸發(fā) | 關(guān)閉時(shí)的點(diǎn)擊位置 (left right cell outside ) |
open | 打開(kāi)時(shí)觸發(fā) | { position: 'left' | 'right' , name: string } |
close | 關(guān)閉時(shí)觸發(fā) | { position: string , name: string } |
beforeClose 的第一個(gè)參數(shù)為對(duì)象,對(duì)象中包含以下屬性:
參數(shù)名 | 說(shuō)明 | 類(lèi)型 |
---|---|---|
name | 標(biāo)識(shí)符 | string |
position | 關(guān)閉時(shí)的點(diǎn)擊位置 (left right cell outside ) | string |
instance | SwipeCell 實(shí)例,用于調(diào)用實(shí)例方法 | SwipeCell |
通過(guò) ref 可以獲取到 SwipeCell 實(shí)例并調(diào)用實(shí)例方法,詳見(jiàn) 組件實(shí)例方法
方法名 | 說(shuō)明 | 參數(shù) | 返回值 |
---|---|---|---|
open | 打開(kāi)單元格側(cè)邊欄 | position: left | right | - |
close | 收起單元格側(cè)邊欄 | - | - |
參見(jiàn)在桌面端使用。
更多建議: