在项目管理中,任务之间的依赖关系(紧前紧后)并非一成不变。传统方式下,开发者需要在 links 数组中手动增删配置,灵活性差。vxe-gantt 提供了可视化创建依赖线和双击删除依赖线的能力,让用户可以直接在甘特图上通过拖拽建立任务关联,或双击已有连线快速移除,大幅提升项目排期的交互效率。
参数说明
实现可视化依赖管理主要依赖以下配置:
| 配置项 | 作用 |
|---|---|
| taskBarConfig.linkCreatable | 是否允许通过拖拽任务条创建新的依赖线 |
| taskLinkConfig.isDblclickToRemove | 是否允许双击依赖线将其删除 |
| taskLinkConfig.isHover | 鼠标悬停依赖线时是否高亮,提升可操作性 |
| taskLinkConfig.isCurrent | 鼠标点击依赖线时是否保持高亮,便于确认选中状态 |
务数据必须配置 rowConfig.keyField(如 id),且 links 中的 from / to 值需与任务主键一致,否则依赖线无法正确关联。
代码

html
<template>
<div>
<vxe-gantt v-bind="ganttOptions"></vxe-gantt>
</div>
</template>
<script setup>
import { reactive } from 'vue'
import { VxeGanttDependencyType } from 'vxe-gantt'
const ganttOptions = reactive({
border: true,
height: 600,
columnConfig: {
resizable: true
},
rowConfig: {
keyField: 'id' // 行主键
},
taskBarConfig: {
showProgress: true, // 是否显示进度条
showContent: true, // 是否在任务条显示内容
moveable: true, // 是否允许拖拽任务移动日期
resizable: true, // 是否允许拖拽任务调整日期
linkCreatable: true, // 是否允许自定义创建依赖线
barStyle: {
round: true, // 圆角
bgColor: '#fca60b', // 任务条的背景颜色
completedBgColor: '#65c16f' // 已完成部分任务条的背景颜色
}
},
taskLinkConfig: {
isHover: true, // 当鼠标移到依赖线时,是否要高亮当前依赖线
isCurrent: true, // 当鼠标点击依赖线时,是否要高亮当前依赖线
isDblclickToRemove: true // 是否允许双击依赖线删除
},
taskViewConfig: {
tableStyle: {
width: 480 // 表格宽度
}
},
links: [
{ from: 10001, to: 10002, type: VxeGanttDependencyType.FinishToFinish },
{ from: 10004, to: 10005, type: VxeGanttDependencyType.StartToStart },
{ from: 10005, to: 10006, type: VxeGanttDependencyType.FinishToStart },
{ from: 10013, to: 10012, type: VxeGanttDependencyType.StartToFinish }
],
columns: [
{ type: 'seq', width: 70 },
{ field: 'title', title: '任务名称' },
{ field: 'start', title: '开始时间', width: 100 },
{ field: 'end', title: '结束时间', width: 100 },
{ field: 'progress', title: '进度(%)', width: 80 }
],
data: [
{ id: 10001, title: '任务1', start: '2024-03-01', end: '2024-03-04', progress: 3 },
{ id: 10002, title: '任务2', start: '2024-03-03', end: '2024-03-08', progress: 10 },
{ id: 10003, title: '任务3', start: '2024-03-03', end: '2024-03-11', progress: 90 },
{ id: 10004, title: '任务4', start: '2024-03-05', end: '2024-03-11', progress: 15 },
{ id: 10005, title: '任务5', start: '2024-03-08', end: '2024-03-15', progress: 100 },
{ id: 10006, title: '任务6', start: '2024-03-10', end: '2024-03-21', progress: 5 },
{ id: 10007, title: '任务7', start: '2024-03-15', end: '2024-03-24', progress: 70 },
{ id: 10008, title: '任务8', start: '2024-03-05', end: '2024-03-15', progress: 50 },
{ id: 10009, title: '任务9', start: '2024-03-19', end: '2024-03-20', progress: 5 },
{ id: 10010, title: '任务10', start: '2024-03-12', end: '2024-03-20', progress: 10 },
{ id: 10011, title: '任务11', start: '2024-03-01', end: '2024-03-08', progress: 90 },
{ id: 10012, title: '任务12', start: '2024-03-03', end: '2024-03-06', progress: 60 },
{ id: 10013, title: '任务13', start: '2024-03-02', end: '2024-03-05', progress: 50 },
{ id: 10014, title: '任务14', start: '2024-03-04', end: '2024-03-15', progress: 0 },
{ id: 10015, title: '任务15', start: '2024-03-01', end: '2024-03-05', progress: 30 }
]
})
</script>
关键点
- 主键一致性
- links 中的 from / to 必须与任务数据的 keyField(本例为 id)严格对应,否则依赖线无法渲染或创建失败。
- 循环依赖检测
- 组件不会自动阻止循环依赖(如 A→B→A),用户可能误操作形成死循环。建议监听依赖线创建事件,在保存前进行拓扑校验。
- 数据同步
- 可视化创建或删除依赖线后,组件内部会自动维护 links 数组。若需持久化到后端,可监听 @link-create 和 @link-remove 事件,或直接获取最新的 links 数据提交。
- 依赖类型自定义
- 拖拽创建时默认使用 FinishToStart 类型。若需支持用户选择其他类型(SS、FF、SF),可结合右键菜单或弹窗,在创建后修改 type 字段。
vxe-gantt 通过 linkCreatable 和 isDblclickToRemove 两个配置,将依赖线的增删从代码层面解放出来,交给了最终用户。配合悬停高亮、点击选中等细节,交互体验流畅自然。开发者只需保证数据主键正确,并处理好变更事件的同步,即可在项目中快速落地这一能力。