logo

X6

  • 文档
  • API
  • 图表示例
  • 常见问题
  • 更新日志
  • XFlow
  • 所有产品antv logo arrow
  • 2.x
  • 简介
  • 快速上手
  • 基础
    • 画布
    • 节点
    • 边
    • 连接桩
    • 交互
    • 事件
    • 数据
  • 进阶
    • 连接点
    • 工具
    • 群组
    • React 节点
    • Vue 节点
    • Angular 节点
    • HTML 节点
  • 插件
    • 图形变换
    • 对齐线
    • 复制粘贴
    • 快捷键
    • 撤销重做
    • 框选
    • 滚动画布
    • 小地图
    • Dnd
    • Stencil
    • 导出
  • 升级到 2.x 版本
  • 开发者工具

Vue 节点

上一篇
React 节点
下一篇
Angular 节点

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

在本章节中,你可以了解到

  • 如何使用 Vue 组件来渲染节点内容
  • 如何更新节点内容

渲染节点

我们提供了一个独立的包 @antv/x6-vue-shape 来使用 Vue 组件渲染节点。

注意

需要注意的是,x6 的版本要和 x6-vue-shape 的版本匹配,也就是两个包需要用同一个大版本。

<template>
<div class="app-content">
<div id="container"></div>
<TeleportContainer />
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import ProgressNode from './components/ProgressNode.vue'
import { Graph } from '@antv/x6'
import { register, getTeleport } from '@antv/x6-vue-shape'
register({
shape: 'custom-vue-node',
width: 100,
height: 100,
component: ProgressNode,
})
const TeleportContainer = getTeleport()
export default defineComponent({
name: 'App',
components: {
TeleportContainer,
},
mounted() {
const graph = new Graph({
container: document.getElementById('container')!,
background: {
color: '#F2F7FA',
},
autoResize: true,
})
graph.addNode({
shape: 'custom-vue-node',
x: 100,
y: 60,
})
},
})
</script>

节点组件内容如下:

<template>
<el-progress type="dashboard" :percentage="percentage" :width="80">
<template #default="{ percentage }">
<span class="percentage-value">%</span>
</template>
</el-progress>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ProgressNode',
inject: ['getNode'],
data() {
return {
percentage: 80,
}
},
mounted() {
const node = (this as any).getNode()
console.log(node)
},
})
</script>

最终效果如下:

更新节点

Vue 节点中更新节点内容,可以使用两种方式:

  • 在组件内部监听节点事件,在外部触发事件。
  • 使用 Vuex 等状态管理器(这里不展开讲,和普通的 Vue 组件一样使用 Vuex 数据)。

下面介绍使用事件的方式来动态更新节点内容:

const node = graph.addNode({
shape: 'custom-vue-node',
x: 100,
y: 60,
data: {
progress: 80,
},
})
setInterval(() => {
const { progress } = node.getData()
node.setData({
progress: (progress + 10) % 100,
})
}, 2000)

在节点组件内部,我们可以监听节点 data 改变事件:

export default defineComponent({
name: 'ProgressNode',
inject: ['getNode'],
data() {
return {
percentage: 80,
}
},
mounted() {
const node = (this as any).getNode() as Node
node.on('change:data', ({ current }) => {
const { progress } = current
this.percentage = progress
})
},
})

Vue2 中使用

上面我们使用到了 teleport,它是 Vue3 中的特性,如果在 Vue2 中,如何使用呢?

<template>
<div id="app"></div>
</template>
<script lang="ts">
import Vue from 'vue'
import CustomNode from './components/CustomNode.vue'
import { Graph } from '@antv/x6'
import { register } from '@antv/x6-vue-shape'
register({
shape: 'custom-vue-node',
width: 100,
height: 100,
component: CustomNode,
})
export default Vue.extend({
name: 'App',
mounted() {
const graph = new Graph({
container: document.getElementById('app'),
width: 1000,
height: 1000,
})
graph.addNode({
shape: 'custom-vue-node',
x: 100,
y: 100,
})
},
})
</script>

节点组件写法和上面的一致。

注意

在 Vue2 中,节点组件内容有一些限制,比如无法使用 Vuex、i18n、element-ui 等。