安装组件(我这里使用的是NPM安装)
npm install sortablejs --save
在需要使用拖拽功能的页面中使用(完整功能代码)
<div class="tag_box"><div class="drag" v-for="(item,index) in taglist" :key="index">{{item.name}}</div>
</div>
import Sortable from "sortablejs";
export default {data() {return {taglist:[{id:1,name:'型号'},{id:2,name:'产牌'},{id:3,name:'价格'},{id:4,name:'批次'},{id:6,name:'有效期'},{id:5,name:'描述'}]}},methods: {async rowDrop() {const that = thisconst tbody = document.querySelector(".tag_box");const sortable = Sortable.create(tbody, {animation: 150, disabled: false, handle: ".drag", chosenClass: "sortable-chosen",filter: ".disabled", onEnd: evt => { let proId = that.id; console.log(evt.oldIndex) console.log(evt.newIndex) }})},},mounted() {this.rowDrop()},components: {Sortable}
}
最终效果如下
一、拖拽前
一、拖拽中
一、拖拽后