Vue 树组件 vxe-tree 任意拖拽子节点
vxe-tree 支持节点拖拽排序,并可进一步开启跨层级拖拽、自身拖到自己的子级、按住 Ctrl/Command 拖成子节点等能力。使用时有两个关键前提需要先明确:拖拽功能仅支持 transform 模式;内置拖拽主要改变展示结构,不会自动改写原始数据源,若需要持久化顺序,应通过 getFullData() 读取当前结构或自行同步数据。
vxe-tree 如何实现任意拖拽子节点?
在树配置中设置 transform: true 与 drag: true 开启拖拽;再通过 dragConfig 按需开启 isCrossDrag(跨层级)、isSelfToChildDrag(自身到子级)、isToChildDrag(Ctrl/Command 拖成子节点)。
- transform 必须为 true。拖拽功能仅支持该模式,非 transform 模式下拖拽不可用。
- drag 必须为 true,这是拖拽总开关。
- 数据需形成正确的父子关系:id 唯一,parentId 指向真实存在的父节点,parentId: null 表示根节点。
- 若需要持久化拖拽结果,不能假设原始 data 数组会自动重排,需通过 getFullData() 或拖拽事件自行处理。
可拖拽节点调整顺序;开启对应配置后,可跨层级拖拽、拖到自身子级、按住 Ctrl/Command 拖成子节点。
四种拖拽能力对照
| 能力 | 配置项 | 操作方式 | 说明 |
|---|---|---|---|
| 基础拖拽排序 | drag: true | 鼠标拖拽 | 调整节点顺序,默认能力 |
| 跨层级拖拽 | dragConfig.isCrossDrag: true | 鼠标拖拽 | 允许在不同父节点之间拖拽 |
| 自身拖到子级 | dragConfig.isSelfToChildDrag: true | 鼠标拖拽 | 允许把节点拖到自己的子级中 |
| 拖拽转成子节点 | dragConfig.isToChildDrag: true | 拖拽时按住 Ctrl 或 Command | 便捷地把节点拖成目标节点的子级 |
- 操作说明:
- 拖拽排序:鼠标拖拽可将节点移动到任意节点位置。
- 拖拽到子级:鼠标拖拽的同时按住 Ctrl(Windows/Linux)或 Command(macOS),移动到目标节点上即可成为其子级。
拖拽调整顺序
<template>
<div>
<div>
<vxe-button status="primary" @click="resultEvent">获取数据</vxe-button>
</div>
<vxe-tree ref="treeRef" v-bind="treeOptions"></vxe-tree>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
const treeRef = ref()
const treeOptions = reactive({
transform: true,
drag: true,
data: [
{ title: '节点2', id: '2', parentId: null },
{ title: '节点3', id: '3', parentId: null },
{ title: '节点3-1', id: '31', parentId: '3' },
{ title: '节点3-2', id: '32', parentId: '3' },
{ title: '节点3-2-1', id: '321', parentId: '32' },
{ title: '节点3-2-2', id: '322', parentId: '32' },
{ title: '节点3-3', id: '33', parentId: '3' },
{ title: '节点3-3-1', id: '331', parentId: '33' },
{ title: '节点3-3-2', id: '332', parentId: '33' },
{ title: '节点3-3-3', id: '333', parentId: '33' },
{ title: '节点3-4', id: '34', parentId: '3' },
{ title: '节点4', id: '4', parentId: null },
{ title: '节点4-1', id: '41', parentId: '4' },
{ title: '节点4-1-1', id: '411', parentId: '42' },
{ title: '节点4-1-2', id: '412', parentId: '42' },
{ title: '节点4-2', id: '42', parentId: '4' },
{ title: '节点4-3', id: '43', parentId: '4' },
{ title: '节点4-3-1', id: '431', parentId: '43' },
{ title: '节点4-3-2', id: '432', parentId: '43' },
{ title: '节点5', id: '5', parentId: null }
]
})
const resultEvent = () => {
const $tree = treeRef.value
if ($tree) {
const treeData = $tree.getFullData()
console.log(treeData)
}
}
</script>
跨层级拖拽
通过 drag-config.isCrossDrag 启用跨层级拖拽

<template>
<div>
<vxe-tree v-bind="treeOptions"></vxe-tree>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const treeOptions = reactive({
transform: true,
drag: true,
dragConfig: {
isCrossDrag: true
},
nodeConfig: {
isHover: true
},
data: [
{ title: '节点2', id: '2', parentId: null },
{ title: '节点3', id: '3', parentId: null },
{ title: '节点3-1', id: '31', parentId: '3' },
{ title: '节点3-2', id: '32', parentId: '3' },
{ title: '节点3-2-1', id: '321', parentId: '32' },
{ title: '节点3-2-2', id: '322', parentId: '32' },
{ title: '节点3-3', id: '33', parentId: '3' },
{ title: '节点3-3-1', id: '331', parentId: '33' },
{ title: '节点3-3-2', id: '332', parentId: '33' },
{ title: '节点3-3-3', id: '333', parentId: '33' },
{ title: '节点3-4', id: '34', parentId: '3' },
{ title: '节点4', id: '4', parentId: null },
{ title: '节点4-1', id: '41', parentId: '4' },
{ title: '节点4-1-1', id: '411', parentId: '42' },
{ title: '节点4-1-2', id: '412', parentId: '42' },
{ title: '节点4-2', id: '42', parentId: '4' },
{ title: '节点4-3', id: '43', parentId: '4' },
{ title: '节点4-3-1', id: '431', parentId: '43' },
{ title: '节点4-3-2', id: '432', parentId: '43' },
{ title: '节点5', id: '5', parentId: null }
]
})
</script>
自己拖拽给自己子级
通过 drag-config.isSelfToChildDrag 启用允许自己拖拽给自己的子级
<template>
<div>
<vxe-tree v-bind="treeOptions"></vxe-tree>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const treeOptions = reactive({
transform: true,
drag: true,
dragConfig: {
isCrossDrag: true,
isSelfToChildDrag: true
},
nodeConfig: {
isHover: true
},
data: [
{ title: '节点2', id: '2', parentId: null },
{ title: '节点3', id: '3', parentId: null },
{ title: '节点3-1', id: '31', parentId: '3' },
{ title: '节点3-2', id: '32', parentId: '3' },
{ title: '节点3-2-1', id: '321', parentId: '32' },
{ title: '节点3-2-2', id: '322', parentId: '32' },
{ title: '节点3-3', id: '33', parentId: '3' },
{ title: '节点3-3-1', id: '331', parentId: '33' },
{ title: '节点3-3-2', id: '332', parentId: '33' },
{ title: '节点3-3-3', id: '333', parentId: '33' },
{ title: '节点3-4', id: '34', parentId: '3' },
{ title: '节点4', id: '4', parentId: null },
{ title: '节点4-1', id: '41', parentId: '4' },
{ title: '节点4-1-1', id: '411', parentId: '42' },
{ title: '节点4-1-2', id: '412', parentId: '42' },
{ title: '节点4-2', id: '42', parentId: '4' },
{ title: '节点4-3', id: '43', parentId: '4' },
{ title: '节点4-3-1', id: '431', parentId: '43' },
{ title: '节点4-3-2', id: '432', parentId: '43' },
{ title: '节点5', id: '5', parentId: null }
]
})
</script>
拖拽转成子节点
通过 drag-config.isToChildDrag 启用便捷拖拽成子节点,拖拽的同时按住 Ctrl 键或 Command 键可以自动拖放到该节点的子级

操作说明
拖拽排序:鼠标拖拽可以将数据拖拽移动到任意节点中
拖拽到子级:鼠标拖拽的同时按住 Ctrl 键或 Command 键移动到子级
<template>
<div>
<vxe-tip status="primary" title="操作说明" icon="vxe-icon-question-circle-fill">
<div>拖拽排序:鼠标拖拽可以将数据拖拽移动到任意节点中</div>
<div>拖拽到子级:鼠标拖拽的同时按住 Ctrl 键或 Command 键移动到子级</div>
</vxe-tip>
<vxe-tree v-bind="treeOptions"></vxe-tree>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const treeOptions = reactive({
transform: true,
drag: true,
dragConfig: {
isCrossDrag: true,
isSelfToChildDrag: true,
isToChildDrag: true
},
nodeConfig: {
isHover: true
},
data: [
{ title: '节点2', id: '2', parentId: null },
{ title: '节点3', id: '3', parentId: null },
{ title: '节点3-1', id: '31', parentId: '3' },
{ title: '节点3-2', id: '32', parentId: '3' },
{ title: '节点3-2-1', id: '321', parentId: '32' },
{ title: '节点3-2-2', id: '322', parentId: '32' },
{ title: '节点3-3', id: '33', parentId: '3' },
{ title: '节点3-3-1', id: '331', parentId: '33' },
{ title: '节点3-3-2', id: '332', parentId: '33' },
{ title: '节点3-3-3', id: '333', parentId: '33' },
{ title: '节点3-4', id: '34', parentId: '3' },
{ title: '节点4', id: '4', parentId: null },
{ title: '节点4-1', id: '41', parentId: '4' },
{ title: '节点4-1-1', id: '411', parentId: '42' },
{ title: '节点4-1-2', id: '412', parentId: '42' },
{ title: '节点4-2', id: '42', parentId: '4' },
{ title: '节点4-3', id: '43', parentId: '4' },
{ title: '节点4-3-1', id: '431', parentId: '43' },
{ title: '节点4-3-2', id: '432', parentId: '43' },
{ title: '节点5', id: '5', parentId: null }
]
})
</script>
关键配置
1. 拖拽总开关与模式限制
transform: true,
drag: true
- transform: true:平铺数据转树形,拖拽功能仅支持该模式。
- drag: true:开启拖拽。未开启时,dragConfig 中的配置均不生效。
2. 跨层级拖拽
dragConfig: {
isCrossDrag: true
}
允许节点在不同父节点之间移动。未开启时,拖拽通常限制在同级或有限范围内。
3. 自身拖到自己的子级
dragConfig: {
isSelfToChildDrag: true
}
允许把节点拖到它自己的子级中。该能力灵活但需注意循环结构风险:节点被拖入自身子树后,可能形成父子引用闭环。业务层应在保存前做防环校验。
4. 拖拽转成子节点
dragConfig: {
isToChildDrag: true
}
开启后,拖拽时按住 Ctrl(Windows/Linux)或 Command(macOS),可直接把节点拖成目标节点的子级,无需先展开再精确落位。
5. 获取当前树结构
const treeData = $tree.getFullData()
- 返回当前树的完整结构。
- 由于内置拖拽不保证改写原始 data 数组,提交前建议用 getFullData() 读取当前展示结构,再同步到业务数据或提交后端。
- 若需要更细粒度的拖拽事件(拖拽开始、结束、落点等),可进一步监听组件事件,以官方文档为准。
配置参数
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| transform | Boolean | false | 平铺数据转树形,拖拽仅支持该模式 |
| drag | Boolean | false | 拖拽总开关 |
| dragConfig.isCrossDrag | Boolean | false | 是否允许跨层级拖拽 |
| dragConfig.isSelfToChildDrag | Boolean | false | 是否允许拖拽自身到自己的子级 |
| dragConfig.isToChildDrag | Boolean | false | 是否允许按住 Ctrl/Command 拖拽成子节点 |
| nodeConfig.isHover | Boolean | false | 鼠标悬停时是否高亮节点 |
| 节点 id | String / Number | — | 节点唯一标识 |
| 节点 parentId | String / Number / null | — | 父节点 id,null 表示根节点 |
| 节点 title | String | — | 节点显示文本 |
| getFullData() | Method | — | 获取当前完整树结构 |

浙公网安备 33010602011771号