vue3+ts使用antv/x6

使用 2.x 版本 x6.antv 新官网:

安装

js 复制代码
npm install @antv/x6
//"@antv/x6": "^2.1.6",

项目结构

1、初始化画布 index.vue
js 复制代码
<template>
    <div id="container"></div>
</template>
    
<script setup lang='ts'>
import { onMounted } from "vue";
import { Graph } from '@antv/x6';
let graph:Graph
const graphInit = ()=>{
    graph = new Graph({
        container: document.getElementById('container')!,
    });
}

onMounted(()=>{
    graphInit()
})
</script>
     
<style scoped>
#container{
    width: 100vw;
    height: 100vh;
}
</style>
2、注册节点

渲染 Vue 节点,这个文档完全够用

js 复制代码
npm install @antv/x6-vue-shape
//"@antv/x6-vue-shape": "^2.0.9",

节点node.vue

js 复制代码
<template>
  <div class="nodeitem">
    {{ data?.nodeName }}
  </div>
</template>
<script setup lang='ts'>
import { inject, onMounted,ref } from "vue";
import { Node } from '@antv/x6'

interface NodeDTO {
  nodeId?: string
  nodeName: string
}

const getNode: Function | undefined = inject<Function>("getNode");
const data =  ref<NodeDTO|undefined>(undefined)
onMounted(() => {
  const node = getNode?.() as Node;
  data.value = node?.getData()
});
</script>
    
<style scoped>
.nodeitem{
  width:100px;
  border: 1px solid #ccc;
}
</style>
3、在画布引入并注册自定义节点,配置节点信息

主画布:index.vue

js 复制代码
<template>
    <div id="container"></div>
    <TeleportContainer/>
</template>
    
<script setup lang='ts'>
import { onMounted } from "vue";
import { Graph,Cell } from '@antv/x6';
import NodeItem from "./node.vue";
import {register,getTeleport} from '@antv/x6-vue-shape'
let graph:Graph
register({
    shape: "node-item",
    width: 150,
    height: 100,
    component: NodeItem,
});// 注册自定义节点
const TeleportContainer = getTeleport();// 自定义节点优化
const refreshData = (data)=>{//渲染节点数据
    const cells: Cell[] = []
    data.nodes.forEach((item: any) => {
        cells.push(graph.createNode(item))
    })
    data.edges?.forEach((item: any) => {
        cells.push(graph.createEdge(item))
    })
    graph.resetCells(cells)
    graph.centerContent()
    graph.zoomToFit({ padding: 10, maxScale: 1 })
}
const graphInit = ()=>{
    graph = new Graph({
        container: document.getElementById('container')!,
    });
    let data = {
        nodes: [
            {
                id: 'node1', // String,可选,节点的唯一标识
                shape: 'node-item',
                x: 40,       // Number,必选,节点位置的 x 值
                y: 40,       // Number,必选,节点位置的 y 值
                data: {
                    nodeId: 'node1',
                    nodeName: '节点1',
                },
            }
        ],
        edges:[]
    }
    refreshData(data)
}
onMounted(()=>{
    graphInit()
})
</script>

展示

小小预告:

下一篇 自定义节点样式

再下一篇 侧边栏

再下一篇 整理画布

相关推荐
ArkPppp2 分钟前
NestJS全栈实战笔记:优雅处理 Entity 与 DTO 的映射与字段过滤
javascript·nestjs
钟智强15 分钟前
React2Shell:CVE-2025-66478 Next.js 远程执行漏洞深度分析与代码剖析
开发语言·javascript·ecmascript
Dragon Wu21 分钟前
Electron Forge集成React Typescript完整步骤
前端·javascript·react.js·typescript·electron·reactjs
华仔啊24 分钟前
jQuery 4.0 发布,IE 终于被放弃了
前端·javascript
未来龙皇小蓝43 分钟前
RBAC前端架构-02:集成Vue Router、Vuex和Axios实现基本认证实现
前端·vue.js·架构
空白诗1 小时前
高级进阶 React Native 鸿蒙跨平台开发:slider 滑块组件 - 进度条与评分系统
javascript·react native·react.js
晓得迷路了1 小时前
栗子前端技术周刊第 116 期 - 2025 JS 状态调查结果、Babel 7.29.0、Vue Router 5...
前端·javascript·vue.js
How_doyou_do1 小时前
执行上下文、作用域、闭包 patch
javascript
叫我一声阿雷吧1 小时前
深入理解JavaScript作用域和闭包,解决变量访问问题
开发语言·javascript·ecmascript
淡忘_cx1 小时前
使用Jenkins自动化部署vue项目(2.528.2版本)
vue.js·自动化·jenkins