跳到主要内容

Vue Gantt 快速上手

注释

本教程涵盖 DHTMLX Gantt 的 Commercial、Enterprise 和 Ultimate 版本中包含的 Vue 封装。 如果您使用的是免费的 Community 版本(v10+)、旧版 GPL 版本(v9.x及更早)或 Individual 版本,请按照替代指南: 如何开始使用 Vue。

Vue Gantt 组件是 DHTMLX Gantt 的官方封装。 本指南将带你创建一个小型的 Vue 3 + Vite 应用,并使用试用包渲染一个基本的甘特图。

如果您刚接触 Vue,请先阅读官方 Vue 文档。

请查看 GitHub 上遵循本教程的完整可运行项目。

Prerequisites​

  • Node.js 已安装
  • npm 或 Yarn
  • Vue 3 项目(本页演示如何用 Vite 创建一个)
  • Vue Gantt 包访问(评估版或专业版)

1. 创建 Vue 3 项目​

npm create vite@latest vue-gantt-quick-start -- --template vue-ts
cd vue-gantt-quick-start
npm install

如果您更习惯使用 Yarn,请将安装步骤替换为 yarn。

2. 安装 Vue Gantt​

请按照 Vue Gantt 安装指南安装 Vue Gantt。

本教程使用评估版软件包:

npm install @dhtmlx/trial-vue-gantt

或

yarn add @dhtmlx/trial-vue-gantt

如果您已在使用专业版软件包,请在命令和导入中将 @dhtmlx/trial-vue-gantt 替换为 @dhx/vue-gantt。

3. 添加演示数据​

创建 src/demoData.ts(对于由外部拥有、保存在 Vue 状态中的数据,建议使用 SerializedTask / SerializedLink):

src/demoData.ts
import type { SerializedLink, SerializedTask } from "@dhtmlx/trial-vue-gantt";

export const tasks: SerializedTask[] = [
{
id: 1,
text: "Office itinerancy",
type: "project",
start_date: new Date(2026, 0, 5),
duration: 10,
progress: 0.4,
open: true,
parent: 0
},
{
id: 2,
text: "Planning",
start_date: new Date(2026, 0, 5),
duration: 4,
progress: 0.6,
parent: 1
}
];

export const links: SerializedLink[] = [{ id: 1, source: 1, target: 2, type: "0" }];

4. 创建 Gantt 组件​

创建 src/components/GanttChart.vue:

src/components/GanttChart.vue
<script setup lang="ts">
import { ref } from "vue";
import VueGantt, {
type SerializedLink,
type SerializedTask,
type VueGanttDataConfig
} from "@dhtmlx/trial-vue-gantt";
import "@dhtmlx/trial-vue-gantt/dist/vue-gantt.css";

import { links as initialLinks, tasks as initialTasks } from "../demoData";

const tasks = ref<SerializedTask[]>(initialTasks);
const links = ref<SerializedLink[]>(initialLinks);

const data: VueGanttDataConfig = {
save: (entity, action, item, id) => {
console.log("save", { entity, action, item, id });
}
};
</script>

<template>
<div style={{height: '100%', width: '100%'}}>
<VueGantt :tasks="tasks" :links="links" :data="data" />
</div>
</template>

如果您使用专业版软件包,请替换这两处导入:

  • @dhtmlx/trial-vue-gantt -> @dhx/vue-gantt
  • @dhtmlx/trial-vue-gantt/dist/vue-gantt.css -> @dhx/vue-gantt/dist/vue-gantt.css

5. 在应用外壳中渲染 Gantt​

替换 src/App.vue:

src/App.vue
<script setup lang="ts">
import GanttChart from "./components/GanttChart.vue";
</script>

<template>
<div style={{height: '100vh', width: '100vw'}}>
<GanttChart />
</div>
</template>

6. 启动应用​

npm run dev

打开本地 Vite URL。您应该能看到一个正常工作的甘特图,并且在编辑任务或链接时会在控制台看到日志。

如果您是将 Gantt 添加到现有应用中,请保留当前的 App.vue 布局,并在目标页面/组件中渲染 <GanttChart />。请确保父级布局为 Gantt 区域提供高度。

7. (可选)用本地保存处理替换日志输出​

当您希望在添加后端或存储之前,让 Vue 状态与图表编辑保持同步时,请使用此方法。更新 src/components/GanttChart.vue。

const data: VueGanttDataConfig = {
save: (entity, action, item, id) => {
if (entity === "task") {
if (action === "create") tasks.value = [...tasks.value, item as SerializedTask];
if (action === "update") tasks.value = tasks.value.map(t => String(t.id) === String(id) ? item as SerializedTask : t);
if (action === "delete") tasks.value = tasks.value.filter(t => String(t.id) !== String(id));
}

if (entity === "link") {
if (action === "create") links.value = [...links.value, item as SerializedLink];
if (action === "update") links.value = links.value.map(l => String(l.id) === String(id) ? item as SerializedLink : l);
if (action === "delete") links.value = links.value.filter(l => String(l.id) !== String(id));
}
}
};

对于涉及多项变更的操作(例如自动排程),建议使用 data.batchSave。

结果​

现在您已有一个 Vue 3 应用,它渲染了官方 Vue Gantt 封装,并具备:

  • 响应式的 tasks 和 links 属性
  • 已导入的封装 CSS
  • 用于处理用户编辑的 data.save 回调

这与 GitHub 演示项目中使用的最小示例相同。

GitHub 演示仓库​

遵循本教程的完整可运行项目已发布在 GitHub 上。

Need help?
Got a question about the documentation? Reach out to our technical support team for help and guidance. For custom component solutions, visit the Services page.