vue3中使用vuedraggable实现拖拽

包安装方式

bash 复制代码
yarn add vuedraggable@next
npm i -S vuedraggable@next

属性说明

如果下面的属性说明未能完全看明,可以看左边的对应的菜单查看详细说明和例子。

完整例子

javascript 复制代码
<template>
  <div class="itxst">
    <div>
      <draggable
        :list="state.list"
        ghost-class="ghost"
        chosen-class="chosenClass"
        animation="300"
        @start="onStart"
        @end="onEnd"
      >
        <template #item="{ element }">
          <div class="item">
            {{ element.name }}
          </div>
        </template>
      </draggable>
    </div>
    <div>{{ state.list }}</div>
  </div>
</template>
<script setup>
import { ref, reactive } from "vue";
import draggable from "vuedraggable";
/*
draggable 对CSS样式没有什么要求万物皆可拖拽
:list="state.list"         //需要绑定的数组
ghost-class="ghost"        //被替换元素的样式
chosen-class="chosenClass" //选中元素的样式
animation="300"            //动画效果
@start="onStart"           //拖拽开始的事件
@end="onEnd"               //拖拽结束的事件
*/
const state = reactive({
  //需要拖拽的数据,拖拽后数据的顺序也会变化
  list: [
    { name: "www.itxst.com", id: 0 },
    { name: "www.baidu.com", id: 1 },
    { name: "www.google.com", id: 2 },
  ],
});

//拖拽开始的事件
const onStart = () => {
  console.log("开始拖拽");
};

//拖拽结束的事件
const onEnd = () => {
  console.log("结束拖拽");
};
</script>
<style scoped>
.itxst {
  width: 600px;
  display: flex;
}
.itxst > div:nth-of-type(1) {
  flex: 1;
}
.itxst > div:nth-of-type(2) {
  width: 270px;
  padding-left: 20px;
}
.item {
  border: solid 1px #eee;
  padding: 6px 10px;
  text-align: left;
}

.item:hover {
  cursor: move;
}
.item + .item {
  margin-top: 10px;
}
.ghost {
  border: solid 1px rgb(19, 41, 239);
}
.chosenClass {
  background-color: #f1f1f1;
}
</style>

如果报 missing required prop: "itemKey"

draggable加上属性

javascript 复制代码
 item-key="id"

文档:vue.draggable.next 中文文档

参考文档:vue3中使用vuedraggable

相关推荐
宁波阿成5 小时前
族谱管理系统架构分析与亮点总结
java·系统架构·vue·ruoyi-vue·族谱
:mnong6 小时前
Claude Code 项目设计分析
typescript·claude code
不想上班只想要钱9 小时前
模板里 item.xxx 报错 ,报 item的类型为未知
前端·vue
一只小阿乐10 小时前
js流式模式输出 函数模式使用
开发语言·javascript·ai·vue·agent·流式数据·node 服务
Cxiaomu12 小时前
像ChatGPT一样逐字输出:React + TypeScript 流式接收与“打字机”效果实现方案
人工智能·react.js·chatgpt·typescript
淡忘_cx13 小时前
解决 Vite EACCES 权限错误:从报错到修复的完整指南
前端·vue
We་ct13 小时前
LeetCode 191. 位1的个数:两种解法详解
前端·算法·leetcode·typescript
坐吃山猪14 小时前
TypeScript编程03-枚举
前端·javascript·typescript
阿珊和她的猫14 小时前
TypeScript中const与readonly的区别与应用解析
javascript·typescript·状态模式
北城笑笑15 小时前
Frontend 与 FPGA 深度融合实战解析:从技术协同到多场景落地( 前端和现场可编程门阵列 )
前端·websocket·3d·vue·fpga