极致的灵活度满足工程美学:用Vue Flow绘制一个完美流程图

目录

极致的灵活度满足工程美学:用Vue Flow绘制一个完美流程图

一、环境要求

二、初识Vue Flow

2.1、安装Vue Flow

2.2、Vue Flow构成

2.3、一个小坑

2.4、入门案例

三、Vue Flow优秀的自定义功能

3.1、引入

3.2、节点与连线的自定义

①打样(做模板)

②模版取名

③替换模版

 3.3、节点与连线的事件

①节点事件

②连线事件

3.4、句柄(handles)

四、一个优质的案例

五、总结


极致的灵活度满足工程美学:用Vue Flow绘制一个完美流程图

        Vue Flow的入门门槛要高不少,毕竟找不到中文文档,反正我是不太喜欢看纯英文的文档的。不过没关系,看完这篇还用不好Vue Flow请来找我麻烦。

一、环境要求

        Node.js v20 或更高版本

        Vue 3.3 或更高版本

        前者,没有装nvm建议装一个nvm,noder没有nvm是行不通的。后者意味着Vue Flow不支持Vue2,据说有些辅助组件可以让部分功能在Vue中使用。

Tips:

        如果把我的源码抄过去都跑不动,要么是依赖没有装好,要么是依赖的版本不对,Vue Flow的版本更迭是会伴随着API的更迭的,所以可以安装特定版本的Vue Flow来解决问题。我的Vue Flow版本是:​v1.39.0​

package.json:

    "@vue-flow/background": "^1.3.0",

    "@vue-flow/controls": "^1.1.2",

    "@vue-flow/core": "^1.38.5",

    "@vue-flow/minimap": "^1.5.0",

二、初识Vue Flow

2.1、安装Vue Flow

        根据自己的包管理器安装

不习惯用add就用install,也没问题

npm add @vue-flow/core

pnpm add @vue-flow/core

yarn add @vue-flow/core

2.2、Vue Flow构成

        在 Vue Flow 中,图由节点和边组成,每个节点或边都需要一个唯一的ID,节点还需要 XY 位置,而边需要 source 和 target 节点 ID,具体参照下方示例中的数据。

2.3、一个小坑

        我最开始在官网上找案例的时候,发现除了教程案例以外的所有多组件案例都跑不动。不要只顾着怀疑自己,因为官网上的案例就是跑不动的。

        原因在于:

为了确保 Vue Flow 正确显示,请确保包含必要的样式。

/* these are necessary styles for vue flow */

@import '@vue-flow/core/dist/style.css';

/* this contains the default theme, these are optional styles */

@import '@vue-flow/core/dist/theme-default.css';

         是的,官网复杂案例全都没有添加这两行代码,估计是写文档的程序员被push的太狠了,抓紧写了几个案例就把代码放上来,没有意识到自己是全局引入(我猜的)了这两行代码,并且没有做任何提示。

        但对于我们来说,只是为了画几个流程图的话,全局引入只会让代码变得冗余。局部引入就够了,把这两行放到style中官网的复杂案例就都可以执行了。

2.4、入门案例

        请注意,这个代码确定能跑,如果跑不动请先检查依赖。

        结构非常简单,定义了一个节点数据数组,一个连线数据数组,然后绑定给VueFlow组件即可。


 
 
  1. <script setup>
  2. import { ref } from "vue";
  3. import { VueFlow } from "@vue-flow/core";
  4. const nodes = ref([
  5. // an input node, specified by using `type: 'input'`
  6. {
  7. id: "1",
  8. type: "input",
  9. position: { x: 250, y: 5 },
  10. // all nodes can have a data object containing any data you want to pass to the node
  11. // a label can property can be used for default nodes
  12. data: { label: "Node 1" },
  13. },
  14. // default node, you can omit `type: 'default'` as it's the fallback type
  15. {
  16. id: "2",
  17. position: { x: 100, y: 100 },
  18. data: { label: "Node 2" },
  19. },
  20. // An output node, specified by using `type: 'output'`
  21. {
  22. id: "3",
  23. type: "output",
  24. position: { x: 400, y: 200 },
  25. data: { label: "Node 3" },
  26. },
  27. // this is a custom node
  28. // we set it by using a custom type name we choose, in this example `special`
  29. // the name can be freely chosen, there are no restrictions as long as it's a string
  30. {
  31. id: "4",
  32. type: "special", // <-- this is the custom node type name
  33. position: { x: 800, y: 200 },
  34. data: {
  35. label: "Node 4",
  36. hello: "world",
  37. },
  38. },
  39. ]);
  40. const edges = ref([
  41. // default bezier edge
  42. // consists of an edge id, source node id and target node id
  43. {
  44. id: "e1->2",
  45. source: "1",
  46. target: "2",
  47. },
  48. // set `animated: true` to create an animated edge path
  49. {
  50. id: "e2->3",
  51. source: "2",
  52. target: "3",
  53. animated: true,
  54. },
  55. // a custom edge, specified by using a custom type name
  56. // we choose `type: 'special'` for this example
  57. {
  58. id: "e3->4",
  59. type: "special",
  60. source: "3",
  61. target: "4",
  62. // all edges can have a data object containing any data you want to pass to the edge
  63. data: {
  64. hello: "world",
  65. },
  66. },
  67. ]);
  68. </script>
  69. <template>
  70. <VueFlow :nodes="nodes" :edges="edges" style="height: 100vh; width: 100vw">
  71. </VueFlow>
  72. </template>
  73. <style>
  74. /* import the necessary styles for Vue Flow to work */
  75. @import "@vue-flow/core/dist/style.css";
  76. /* import the default theme, this is optional but generally recommended */
  77. @import "@vue-flow/core/dist/theme-default.css";
  78. </style>

三、Vue Flow优秀的自定义功能

3.1、引入

        首先,预定义的配置项就不介绍了,上面的案例基本就是全部的预定义配置了,这一点和mermaid完全无法相比,但是你要用预定义的配置为什么不直接用mermaid呢?那个更简单清爽。

        Vue Flow的特点就是,什么都可以要,什么都要自己写。

        相比于mermaid来说,Vue Flow接受的数据更复杂冗长,并且预定义的内容极少,连默认的布局都没有(节点通过position控制位置,很容易重叠)。但是Vue Flow提供的自定义API非常丰富并且强大。

3.2、节点与连线的自定义

        自定义三步走:打样取名做替换。

①打样(做模板)

        做一个节点或者连线的模版,比如我需要一个长得像太阳的节点,那么我就写一个组件,然后把这个组件做成一个太阳的样子。

②模版取名

        给自己做的模版取一个名字,比如就叫sun,然后将想要使用这个模版的节点的type属性全部改成"sun"。

③替换模版

        在<VueFlow>标签之间添加如下代码,主要关注插槽名称“#node-sun”意味着所有type为sun的节点都使用这个插槽的模版。比如我设计了一种连线取名为darge,那么插槽名就是“#edge-darge”。


 
 
  1. // 在script中引入
  2. import sunNode from "./sunNode.vue";
  3. <template>
  4. <div class="layout-flow">
  5. <VueFlow
  6. :nodes= "nodes"
  7. :edges= "edges"
  8. @ nodes-initialized= "layoutGraph('TB')"
  9. >
  10. <template #node-sun="props">
  11. <div class="sunNode">
  12. <span>{{ props.data.label }} </span>
  13. </div>
  14. </template>
  15. </VueFlow>
  16. </div>
  17. </template>
 3.3、节点与连线的事件

        该有的事件应有尽有,不得不佩服Vue Flow的完善与强大。

①节点事件

        这是我比较建议的用法,比较简洁


 
 
  1. <script setup>
  2. import { ref } from 'vue'
  3. import { VueFlow } from '@vue-flow/core'
  4. const nodes = ref([
  5. {
  6. id: '1',
  7. data: { label: 'Node 1' },
  8. position: { x: 50, y: 50 },
  9. },
  10. ])
  11. function logEvent( name, data) {
  12. console. log(name, data)
  13. }
  14. </script>
  15. <template>
  16. <!-- bind listeners to the event handlers -->
  17. <VueFlow
  18. :nodes= "nodes"
  19. @ node-drag-start= "logEvent('drag start', $event)"
  20. @ node-drag= "logEvent('drag', $event)"
  21. @ node-drag-stop= "logEvent('drag stop', $event)"
  22. @ node-click= "logEvent('click', $event)"
  23. @ node-double-click= "logEvent('dblclick', $event)"
  24. @ node-contextmenu= "logEvent('contextmenu', $event)"
  25. @ node-mouse-enter= "logEvent('mouseenter', $event)"
  26. @ node-mouse-leave= "logEvent('mouseleave', $event)"
  27. @ node-mouse-move= "logEvent('mousemove', $event)"
  28. />
  29. </template>

        也可以使用useVueFlow这个API,将这些事件转化为钩子,我觉得优势不明显,写起来也冗余。


 
 
  1. <script setup>
  2. import { ref } from 'vue'
  3. import { VueFlow, useVueFlow } from '@vue-flow/core'
  4. // useVueFlow provides access to the event handlers
  5. const {
  6. onNodeDragStart,
  7. onNodeDrag,
  8. onNodeDragStop,
  9. onNodeClick,
  10. onNodeDoubleClick,
  11. onNodeContextMenu,
  12. onNodeMouseEnter,
  13. onNodeMouseLeave,
  14. onNodeMouseMove
  15. } = useVueFlow()
  16. const nodes = ref([
  17. {
  18. id: '1',
  19. data: { label: 'Node 1' },
  20. position: { x: 50, y: 50 },
  21. },
  22. ])
  23. // bind listeners to the event handlers
  24. onNodeDragStart( (event) => {
  25. console. log( 'Node drag started', event)
  26. })
  27. onNodeDrag( (event) => {
  28. console. log( 'Node dragged', event)
  29. })
  30. onNodeDragStop( (event) => {
  31. console. log( 'Node drag stopped', event)
  32. })
  33. // ... and so on
  34. </script>
  35. <template>
  36. <VueFlow :nodes="nodes" />
  37. </template>
②连线事件

        连线事件也可以使用useVueFlow这个API,用法和上方一样,我就不赘述了,这里展示一下有哪些事件API。


 
 
  1. <script setup>
  2. import { ref } from 'vue'
  3. import { VueFlow } from '@vue-flow/core'
  4. const nodes = ref([
  5. {
  6. id: '1',
  7. position: { x: 50, y: 50 },
  8. data: { label: 'Node 1', },
  9. },
  10. {
  11. id: '2',
  12. position: { x: 50, y: 250 },
  13. data: { label: 'Node 2', },
  14. },
  15. ])
  16. const edges = ref([
  17. {
  18. id: 'e1->2',
  19. source: '1',
  20. target: '2',
  21. },
  22. ])
  23. function logEvent( eventName, data) {
  24. console. log(eventName, data)
  25. }
  26. </script>
  27. <template>
  28. <VueFlow
  29. :nodes= "nodes"
  30. :edges= "edges"
  31. @ edge-click= "logEvent('edge clicked', $event)"
  32. @ edge-double-click= "logEvent('edge double clicked', $event)"
  33. @ edge-context-menu= "logEvent('edge context menu', $event)"
  34. @ edge-mouse-enter= "logEvent('edge mouse enter', $event)"
  35. @ edge-mouse-leave= "logEvent('edge mouse leave', $event)"
  36. @ edge-mouse-move= "logEvent('edge mouse move', $event)"
  37. @ edge-update-start= "logEvent('edge update start', $event)"
  38. @ edge-update= "logEvent('edge update', $event)"
  39. @ edge-update-end= "logEvent('edge update end', $event)"
  40. />
  41. </template>
3.4、句柄(handles)

        句柄是通常放置在节点边界上的小圆圈。它们用于通过将连接线从一个手柄拖动到另一个手柄来将节点连接在一起,从而在节点之间形成连接(边缘)。句柄是 VueFlow 的重要组成部分,因为它们是用户在节点之间创建边的主要交互点。如果没有句柄,基本上不可能在节点之间创建边,因为句柄用于计算边的源点和目标点。

        简单来说,就是两个节点连接需要通过句柄,如果没有句柄就无法连接。句柄就是连线两端的端点。常用在自定义节点的组件中,用来指定连线的起始点。

        句柄handdle需要设置两个属性,一个是类型(source/target),一个是位置(Top、Right、Bottom、Left),示例如下:


 
 
  1. <script setup>
  2. import { Handle } from '@vue-flow/core'
  3. defineProps([ 'id', 'sourcePosition', 'targetPosition', 'data'])
  4. </script>
  5. <template>
  6. <Handle type="source" :position="sourcePosition" />
  7. <span>{{ data.label }} </span>
  8. <Handle type="target" :position="targetPosition" />
  9. </template>

四、一个优质的案例

        案例具体代码请访问:Layouting | Vue Flow

        将所有组件代码复制到您的项目中即可,请注意相对路径关系,如果您没有全局引入2.3部分的内容,请在style中添加:


 
 
  1. <style>
  2. @ import "@vue-flow/core/dist/style.css";
  3. @ import "@vue-flow/core/dist/theme-default.css";
  4. // ...其他的css样式
  5. </style>

五、总结

        相比于mermaid,Vue Flow将注意力更多的转到灵活自定义的接口而不是大量的预定义模版,“什么都可以有,什么都要自己写。”是Vue Flow的典型特征。如果您想发挥自己天马行空的想象力,或者满足复杂的项目流程需求,Vue Flow一定能满足您的预期。

        博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that! Here's an example of how you can use Vue 3 and Element Plus to draw a flowchart: First, make sure you have Vue 3 and Element Plus installed in your project. You can install them using npm or yarn: ```bash npm install vue@next npm install element-plus ``` Next, create a new Vue component for your flowchart. Let's call it `Flowchart.vue`: ```vue <template> <div> <el-row> <el-col :span="12"> <el-card> <div class="flowchart-container"> <svg ref="svg"></svg> </div> </el-card> </el-col> </el-row> </div> </template> <script> import { ref, onMounted } from 'vue'; export default { name: 'Flowchart', mounted() { onMounted(() => { this.drawFlowchart(); }); }, methods: { drawFlowchart() { // Get the SVG container const svgContainer = this.$refs.svg; // Create your flowchart using SVG elements // Add your logic here to create the flowchart elements // Example: Create a rectangle const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect'); rect.setAttribute('x', '50'); rect.setAttribute('y', '50'); rect.setAttribute('width', '100'); rect.setAttribute('height', '50'); rect.setAttribute('fill', 'blue'); // Append the rectangle to the SVG container svgContainer.appendChild(rect); }, }, }; </script> <style scoped> .flowchart-container { height: 400px; } </style> ``` In this example, we use Element Plus to create a layout with a card component. Inside the card, we have a container for the SVG element, which will hold our flowchart elements. The `drawFlowchart` method is called when the component is mounted, where you can add your logic to create the flowchart elements using SVG. Remember to import and register the `Flowchart` component in your main Vue instance. Now, you can use the `Flowchart` component in your app: ```vue <template> <div> <Flowchart /> </div> </template> <script> import Flowchart from './Flowchart.vue'; export default { components: { Flowchart, }, }; </script> ``` This is a basic example to get you started. You can customize and enhance it according to your specific requirements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值