27.(vue3.x+vite)以pinia为中心的开发模板(监听watch)

效果截图

在这里插入图片描述

代码实现:

HelloWorld.vue

<template>
  <div style="padding: 20px">
    介绍:<br />
    1:使用统一的 watch 来监听store的值。<br 
要实现完整的流程图,您可以使用以下技术和工具: 1. Vue 3Vue 3 是一个优秀的 JavaScript 框架,可以使您的应用程序更加可维护和易于开发。它具有响应式数据绑定、组件化开发、虚拟 DOM 等特性。 2. ViteVite 是一个快速的开发工具,可帮助您更快地构建 Web 应用程序。它可以快速地启动开发服务器,支持热模块替换和快速构建。 3. Element Plus:Element Plus 是一组基于 Vue 3 的 UI 组件库,可以帮助您快速构建漂亮的界面。 4. Jsplumb:Jsplumb 是一个流程图库,可以帮助您构建复杂的流程图。它支持拖放、连接线、锚点等功能。 5. PiniaPinia 是一个简单、灵活和可扩展的状态管理库,可以帮助您管理应用程序的状态。 6. Sortable:Sortable 是一个 JavaScript 库,可以使您的列表可排序。它支持拖放、滑动等功能。 下面是一个简单的示例,演示如何使用这些技术和工具来构建一个完整的流程图: 1. 首先,您需要安装 Vite,可以使用以下命令: ``` npm install -g vite ``` 2. 创建一个新的 Vue 3 项目,可以使用以下命令: ``` npm init vite-app my-project ``` 3. 安装 Element Plus、Jsplumb、Pinia 和 Sortable,可以使用以下命令: ``` npm install element-plus jsplumb pinia sortable ``` 4. 在 App.vue 文件中,添加一个包含 jsplumb 实例的 div 元素: ```vue <template> <div id="app"> <div id="jsplumb"></div> </div> </template> ``` 5. 在 setup 方法中,创建一个 jsplumb 实例并将其挂载到 div 元素上: ```vue <script> import jsPlumb from &#39;jsplumb&#39; export default { setup() { const jsplumbInstance = new jsPlumb.jsPlumb() jsplumbInstance.setContainer(&#39;jsplumb&#39;) return { jsplumbInstance, } }, } </script> ``` 6. 创建一个 Flowchart 组件,在其中添加一个包含 Sortable 列表的 div 元素: ```vue <template> <div class="flowchart"> <div class="flowchart-nodes" ref="nodes"> <div v-for="node in nodes" :key="node.id" class="flowchart-node"> {{ node.label }} </div> </div> <div class="flowchart-connections" ref="connections"></div> </div> </template> ``` 7. 在 setup 方法中,使用 Pinia 创建一个 store,用于管理节点和连接线的状态: ```vue <script> import { defineComponent } from &#39;vue&#39; import { createPinia } from &#39;pinia&#39; const store = createPinia() export default defineComponent({ setup() { store.state.nodes = [] store.state.connections = [] return { store, } }, }) </script> ``` 8. 在 mounted 方法中,使用 Sortable 初始化节点列表: ```vue <script> import { defineComponent, onMounted } from &#39;vue&#39; import Sortable from &#39;sortablejs&#39; // ... export default defineComponent({ setup() { // ... onMounted(() => { Sortable.create(nodes.value, { group: &#39;nodes&#39;, sort: true, animation: 150, onEnd: (evt) => { store.state.nodes.splice(evt.newIndex, 0, store.state.nodes.splice(evt.oldIndex, 1)[0]) }, }) }) return { // ... } }, }) </script> ``` 9. 创建一个 Node 组件,在其中添加一个包含节点内容的 div 元素: ```vue <template> <div class="node"> <div class="node-content"> <slot /> </div> </div> </template> ``` 10. 在 Flowchart 组件中,使用 Node 组件来渲染节点: ```vue <template> <div class="flowchart"> <div class="flowchart-nodes" ref="nodes"> <Node v-for="node in store.state.nodes" :key="node.id"> {{ node.label }} </Node> </div> <div class="flowchart-connections" ref="connections"></div> </div> </template> ``` 11. 创建一个 Connection 组件,在其中添加一个包含连接线的 div 元素: ```vue <template> <div class="connection"> <div class="connection-line" ref="line"></div> </div> </template> ``` 12. 在 Flowchart 组件中,使用 Connection 组件来渲染连接线: ```vue <template> <div class="flowchart"> <div class="flowchart-nodes" ref="nodes"> <Node v-for="node in store.state.nodes" :key="node.id"> {{ node.label }} </Node> </div> <div class="flowchart-connections" ref="connections"> <Connection v-for="connection in store.state.connections" :key="connection.id" /> </div> </div> </template> ``` 13. 在 setup 方法中,使用 jsplumb 实例初始化连接线: ```vue <script> import { defineComponent } from &#39;vue&#39; import jsPlumb from &#39;jsplumb&#39; // ... export default defineComponent({ setup() { // ... onMounted(() => { jsplumbInstance.ready(() => { store.watch( () => store.state.connections, () => { store.state.connections.forEach((connection) => { const sourceNode = jsplumbInstance.getEndpoints(connection.sourceNodeId)[0] const targetNode = jsplumbInstance.getEndpoints(connection.targetNodeId)[0] const jsplumbConnection = jsplumbInstance.connect({ source: sourceNode, target: targetNode, anchors: [&#39;Right&#39;, &#39;Left&#39;], endpoint: &#39;Blank&#39;, connector: [&#39;Flowchart&#39;, { cornerRadius: 5 }], paintStyle: { stroke: &#39;#5c5c5c&#39;, strokeWidth: 2, }, }) connection.id = jsplumbConnection.id }) }, { immediate: true } ) }) }) return { // ... } }, }) </script> ``` 14. 在 Node 组件上,使用 jsplumb 实例初始化可拖动和可连接的锚点: ```vue <script> import { defineComponent, onMounted } from &#39;vue&#39; import jsPlumb from &#39;jsplumb&#39; export default defineComponent({ setup(props, { slots }) { const endpoint = jsplumbInstance.addEndpoint(self.value, { anchor: [&#39;Right&#39;, &#39;Left&#39;], endpoint: &#39;Blank&#39;, connector: [&#39;Flowchart&#39;, { cornerRadius: 5 }], paintStyle: { stroke: &#39;#5c5c5c&#39;, strokeWidth: 2, }, isSource: true, isTarget: true, }) onMounted(() => { jsplumbInstance.draggable(self.value, { grid: [10, 10], }) }) return { endpoint, } }, }) </script> ``` 15. 在 Node 组件上,使用 jsplumb 实例监听锚点连接事件,并在连接时更新 store 中的连接线状态: ```vue <script> // ... export default defineComponent({ setup(props, { slots }) { // ... endpoint.bind(&#39;connection&#39;, (connection) => { const sourceNodeId = connection.sourceId.split(&#39;-&#39;)[1] const targetNodeId = connection.targetId.split(&#39;-&#39;)[1] store.state.connections.push({ id: null, sourceNodeId, targetNodeId, }) }) return { endpoint, } }, }) </script> ``` 通过以上步骤,您可以使用 Vite、Element Plus、Jsplumb、Pinia 和 Sortable 来构建一个完整的流程图应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地图之家家长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值