最近项目中有一个需求,需要拖拽tab栏的顺序 来实现tab栏对应内容数组的顺序改变,在现有ui库中没有找到,就自己开发了一个 简单记录一下。
两种常用的拖拽插件
1、awe-dnd
github地址:https://github.com/hilongjw/vue-dragging
2、vue-draggable
github地址:https://github.com/SortableJS/Vue.Draggable
通过使用过发现vue-draggable 只能实现tab栏列表拖拽,tab对应的内容顺序不能改变。awe-dnd 中可以使用一个数组,这样 tab改变 列表也会改变,所以选择awe-dnd 。
awe-dnd使用方法
1、安装
npm install awe-dnd --save
2、引入
在 main.js 文件中
import VueDND from 'awe-dnd'
Vue.use(VueDND)
3、API
Vue指令的方式调用
v-dragging="{ list: tabList, item: item, group: 'item' }"
4、Arguments
名称 | 类型 | 默认值 | 说明 |
---|---|---|---|
list | Array | – | 可拖拽对象的数组 |
item | Object | – | 每一个可拖拽的对象 |
group | String | – | unique key |
可拖拽tab栏组件实例
效果:
代码:
<template>
<div>
<div class="tabBox">
<div
v-for="(item,index) in tabList"
v-dragging="{ list: tabList, item: item, group: 'item' }"
:key="index"
class="tab"
:class="{tabActive:item.tabIdx === tabNum}"
@click="onTabClick(item,index)"
>
{
{
item.name}}
<Icon type="ios-close" size="20" class="icon" @click.stop="onTabDelete(item,index)" />
</div>
<Button @click