Vue穿梭框通过作用域插槽实现左右列表自定义模板,左侧传item/index/moveToRight,右侧传item/index/moveToLeft,插槽名建议left-item/right-item,子组件提供默认内容兜底,父组件用v-slot解构使用。
Vue 组件插槽实现穿梭框左右列表项的自定义模板,核心是用 作用域插槽(scoped slot) 把每条数据项和操作方法暴露给父组件,让使用者自由控制渲染结构。
在穿梭框子组件中,左右两个列表(如 leftList 和 rightList)分别用 v-for 渲染时,为每个 <slot> 传入当前项和上下文信息:
item、index、moveToRight 方法item、index、moveToLeft 方法item,通过属性区分方向,例如 left-item 和 right-item,更语义化在子组件模板中,这样写左右列表项的插槽:
<div class="list left"><div v-for="(item, i) in leftList" :key="item.id || i"><slot name="left-item" :item="item" :index="i" :move="() => moveToRight(item)">{{ item.label || item.name }}</slot></div></div><div class="list right"><div v-for="(item, i) in rightList" :key="item.id || i"><slot name="right-item" :item="item" :index="i" :move="() => moveToLeft(item)">{{ item.label || item.name }}</slot></div></div>
注意:默认内容(如 {{ item.label }})作为兜底展示,方便快速使用;作用域属性确保父组件能访问数据和操作。
在调用穿梭框的地方,用 v-slot:left-item 和 v-slot:right-item 定制每一项:
<transfer-box :data="options"><template #left-item="{ item, move }"><div class="custom-item"><span class="avatar">{{ item.avatar }}这样左右两边可完全独立定制结构、样式和交互逻辑,比如加头像、图标、状态标签等。
若需支持勾选再批量移动,可在作用域中增加 checked 状态和 toggle 方法:
leftChecked / rightChecked 数组checked 布尔值和 toggle 函数@change="toggle"
moveSelectedToRight 等批量方法供外部调用这样既保持插槽灵活性,又不破坏原有单条操作逻辑。
tlwdr7650路由器没有wps按钮(tlwdr7650路由器没有wps按钮怎么办)
tlwdr7632扩展器电脑怎么设置(tlwdr7632扩展器电脑设置方法)
tlwda6332re安装教程(tlwda6332re如何安装)
tlwdr7632扩展器手机怎么设置(tlwdr7632扩展器手机设置方法)
tlxdr3010怎么设置网速快(tlxdr3010网速快设置方法)
tlwdr5620易展版怎么克隆(tlwdr5620易展版克隆方法)