跳至主要內容

入门

LincZero大约 5 分钟

入门

本指南涵盖了设置和使用 Vue Flow 的基础知识。您将学习如何安装 Vue Flow、配置它并创建您的第一个流程图。

Note

如果你正在寻找有关如何设置 Vue 项目的指南,请查看官方 Vue 文档open in new window

先决条件 ​

在系好安全带之前,请确保您已配备:

代码沙盒 ​

如果您正在寻找一种快速入门的方法,请查看CodeSandbox 模板open in new window

安装 ​

使用您喜欢的包管理器来安装 Vue Flow:

npm
$ npm add @vue-flow/core

快速入门

在 Vue Flow 中,图由节点和边组成。

每个节点或边都需要一个唯一的 ID。

节点还需要 XY 位置,而边需要源节点 ID 和目标节点 ID。

Note

要确保正确显示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';

有关更多信息,请参阅 主题open in new window 部分

这里有一个简单的例子让你开始使用:

(译者:这里仅翻译ts的代码注释)

js
<script setup>
import { ref } from 'vue'
import { VueFlow } from '@vue-flow/core'

// these components are only shown as examples of how to use a custom node or edge
// you can find many examples of how to create these custom components in the examples page of the docs
import SpecialNode from './components/SpecialNode.vue'
import SpecialEdge from './components/SpecialEdge.vue'

// these are our nodes
const nodes = ref([
  // an input node, specified by using `type: 'input'`
  { 
    id: '1',
    type: 'input', 
    position: { x: 250, y: 5 },
    // all nodes can have a data object containing any data you want to pass to the node
    // a label can property can be used for default nodes
    data: { label: 'Node 1' },
  },

  // default node, you can omit `type: 'default'` as it's the fallback type
  { 
    id: '2', 
    position: { x: 100, y: 100 },
    data: { label: 'Node 2' },
  },

  // An output node, specified by using `type: 'output'`
  { 
    id: '3', 
    type: 'output', 
    position: { x: 400, y: 200 },
    data: { label: 'Node 3' },
  },

  // this is a custom node
  // we set it by using a custom type name we choose, in this example `special`
  // the name can be freely chosen, there are no restrictions as long as it's a string
  {
    id: '4',
    type: 'special', // <-- this is the custom node type name
    position: { x: 400, y: 200 },
    data: {
      label: 'Node 4',
      hello: 'world',
    },
  },
])

// these are our edges
const edges = ref([
  // default bezier edge
  // consists of an edge id, source node id and target node id
  { 
    id: 'e1->2',
    source: '1', 
    target: '2',
  },

  // set `animated: true` to create an animated edge path
  { 
    id: 'e2->3',
    source: '2', 
    target: '3', 
    animated: true,
  },

  // a custom edge, specified by using a custom type name
  // we choose `type: 'special'` for this example
  {
    id: 'e3->4',
    type: 'special',
    source: '3',
    target: '4',

    // all edges can have a data object containing any data you want to pass to the edge
    data: {
      hello: 'world',
    }
  },
])
</script>

<template>
  <VueFlow :nodes="nodes" :edges="edges">
    <!-- bind your custom node type to a component by using slots, slot names are always `node-<type>` -->
    <template #node-special="specialNodeProps">
      <SpecialNode v-bind="specialNodeProps" />
    </template>

    <!-- bind your custom edge type to a component by using slots, slot names are always `edge-<type>` -->
    <template #edge-special="specialEdgeProps">
      <SpecialEdge v-bind="specialEdgeProps" />
    </template>
  </VueFlow>
</template>

<style>
/* import the necessary styles for Vue Flow to work */
@import '@vue-flow/core/dist/style.css';

/* import the default theme, this is optional but generally recommended */
@import '@vue-flow/core/dist/theme-default.css';
</style>

TypeScript

由于 Vue Flow 完全用 TypeScript 编写,因此我们强烈建议使用 TypeScript 来改善开发人员体验并防止常见错误。必要的类型定义包含在库中。

有关更多信息,请查看我们的 TypeDocsopen in new window 文档。