|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +--- |
| 4 | + |
| 5 | +# Events |
| 6 | + |
| 7 | +VueFlow provides a set of events that you can listen to in order to react to changes in the flow. |
| 8 | + |
| 9 | +A full list of events can be found in the [API Reference](/typedocs/interfaces/FlowEvents.html). |
| 10 | + |
| 11 | +## Listening to Events |
| 12 | + |
| 13 | +### VueFlow Component |
| 14 | + |
| 15 | +You can listen to events on the VueFlow component by using the `@` directive: |
| 16 | + |
| 17 | +```vue |
| 18 | +<script setup> |
| 19 | +import { ref } from 'vue'; |
| 20 | +import { VueFlow } from '@vue-flow/core'; |
| 21 | +
|
| 22 | +const nodes = ref([/* ... */]); |
| 23 | +const edges = ref([/* ... */]); |
| 24 | +
|
| 25 | +// Node click event handler |
| 26 | +function onNodeClick({ event, node }) { |
| 27 | + console.log('Node clicked:', node, event); |
| 28 | +} |
| 29 | +
|
| 30 | +// Edge click event handler |
| 31 | +function onEdgeClick({ event, edge }) { |
| 32 | + console.log('Edge clicked:', edge, event); |
| 33 | +} |
| 34 | +</script> |
| 35 | +
|
| 36 | +<template> |
| 37 | + <VueFlow :nodes="nodges" :edges="edges" @node-click="onNodeClick" @edge-click="onEdgeClick"></VueFlow> |
| 38 | +</template> |
| 39 | +``` |
| 40 | + |
| 41 | +### Flow Instance / `useVueFlow` |
| 42 | + |
| 43 | +You can also listen to events on the Flow instance by using the event hooks. |
| 44 | + |
| 45 | +All events are available from `useVueFlow` as `on<EventName>`. For example, the `node-click` event is available as `onNodeClick`. |
| 46 | + |
| 47 | +```vue |
| 48 | +<script setup> |
| 49 | +import { ref } from 'vue'; |
| 50 | +import { VueFlow, useVueFlow } from '@vue-flow/core'; |
| 51 | +
|
| 52 | +const nodes = ref([/* ... */]); |
| 53 | +const edges = ref([/* ... */]); |
| 54 | +
|
| 55 | +// All events are available from `useVueFlow` as `on<EventName>` |
| 56 | +const { onNodeClick, onEdgeClick } = useVueFlow(); |
| 57 | +
|
| 58 | +// Node click event handler |
| 59 | +onNodeClick(({ event, node }) => { |
| 60 | + console.log('Node clicked:', node, event); |
| 61 | +}); |
| 62 | +
|
| 63 | +// Edge click event handler |
| 64 | +onEdgeClick(({ event, edge }) => { |
| 65 | + console.log('Edge clicked:', edge, event); |
| 66 | +}); |
| 67 | +</script> |
| 68 | +
|
| 69 | +<template> |
| 70 | + <VueFlow :nodes="nodges" :edges="edges" @node-click="onNodeClick" @edge-click="onEdgeClick"></VueFlow> |
| 71 | +</template> |
| 72 | +``` |
0 commit comments