记录时间:2024-02-28
一、useFlowStore模块实现
store/useFlowStore.ts
import { defineStore } from 'pinia' import { Flow, Flows } from '@/types/flow'export const useFlowStore = defineStore('flow', { state: () => ({ flow: {} as Flow // 使用类型断言初始化,现在这是安全的,因为所有属性都是可选的 }), actions: { parseFlow(flows: Flows, flowKey: string) { if (!flows || !Array.isArray(flows.root.flow) || !flowKey) { return // 提前返回以避免无效操作 }
this.flow = {} as Flow // 重置flow状态,确保它是一个空对象(符合Flow的类型定义)
const flowList = flows.root.flow // 直接从flows中获取flow数组 const foundFlow = flowList.find( (flow) => flow.key === flowKey && flow.use === '1' ) // 查找符合条件的flow
if (foundFlow) { // 如果找到了匹配的flow,则更新状态 this.flow = { ...foundFlow, // 使用展开语法复制找到的flow的属性 pages: Array.isArray(foundFlow.pages) ? foundFlow.pages : [] // 如果不是数组,则设置为空数组 } } else { // 可以考虑重置flow到初始状态或保持原样,取决于业务需求 this.flow = {} } return this.flow } }, getters: { // 保留特定的 getter,以便直接访问常用属性 getFlow: (state) => state.flow, getKey: (state) => state.flow.key, getName: (state) => state.flow.name, getValue: (state) => state.flow.value, getComment: (state) => state.flow.comment, getRemark: (state) => state.flow.remark, getPages: (state) => state.flow.pages || [] // 确保总是返回一个数组,即使是空的 } })
二、调用示例
无
三、运行测试
无
翻译
搜索
复制
<iframe></iframe> 标签:web,ssts,hospital,flow,flows,state,useFlowStore,Flow,foundFlow From: https://www.cnblogs.com/lizhigang/p/18040727