记录时间:2024-02-28
一、useDictStore模块实现
const/index.ts
// 常量 const DICT_VERSIONDATA = 'versionData' const DICT_PAGE = 'page' const DICT_COMMON = 'common' const DICT_DEVICE = 'device' const DICT_SYSTEM = 'system' const DICT_STATIC = 'static' const DICT_NULL = 'null' const DICT_CONFIG = 'config' const DICT_OUT = 'out' const DICT_MANAGE = 'manage'export { DICT_VERSIONDATA, DICT_PAGE, DICT_COMMON, DICT_DEVICE, DICT_SYSTEM, DICT_STATIC, DICT_NULL, DICT_CONFIG, DICT_OUT, DICT_MANAGE }
store/useDictStore.ts
import { defineStore } from 'pinia' import { Dict } from '@/types/dict' import { DICT_VERSIONDATA, DICT_PAGE, DICT_COMMON, DICT_DEVICE, DICT_SYSTEM, DICT_STATIC, DICT_NULL, DICT_CONFIG, DICT_OUT, DICT_MANAGE } from '@/const' type DictKey = keyof Dictexport const useDataStore = defineStore('data', { state: () => ({ dict: {} as Dict // 使用类型断言初始化,现在这是安全的,因为所有属性都是可选的 }), actions: { // 初始化字典 initDict() { this.dict = { version: '', versionData: {}, terminal: {}, operatorId: '', terminalIp: '', deviceList: [], flowKey: '', flow: {}, linkFlowKey: '', linkFlowPage: '', tradeId: '', page: {}, common: {}, device: {}, system: {}, staticValue: {}, nullValue: {}, config: {}, out: {}, msg: '', error: '', voice: '', receiptList: [], manage: {}, DataDictPageLoadFlag: false, FrontManageFlag: false, BankSignInFlag: false, NetOnlineFlag: true, BasePageDir: '', BizPageDir: '', YBSignNo: '', YBSignDate: '', ReceiptPrinterPaperIndexList: [], LabelPrinterPaperIndexList: [], DocumentPrinterPaperIndexList: [] } },
// 通用设置方法 setDictValue(key: DictKey, value: any) { if (Object.prototype.hasOwnProperty.call(this.dict, key)) { this.dict[key] = value } else { console.warn(`Unknown key: ${key}`) } },
// 针对特定字典类型的设置方法 syncData(dictType: string, key: string, value: any) { const dictMap: { [key: string]: any } = { // 注意:这里需要确保dict中的属性与DICT_*常量对应 [DICT_VERSIONDATA]: this.dict.versionData, [DICT_PAGE]: this.dict.page, [DICT_COMMON]: this.dict.common, [DICT_DEVICE]: this.dict.device, [DICT_SYSTEM]: this.dict.system, [DICT_STATIC]: this.dict.staticValue, [DICT_NULL]: this.dict.nullValue, [DICT_CONFIG]: this.dict.config, [DICT_OUT]: this.dict.out, [DICT_MANAGE]: this.dict.manage }
const targetDict = dictMap[dictType] if (targetDict && key) { targetDict[key] = value } else { console.warn(`Unknown dictType: ${dictType} or invalid key: ${key}`) } },
// 设置凭条信息 setReceiptList(receiptList: any) { this.dict.receiptList = receiptList } }, getters: { // 获取版本信息 getVersion: (state) => state.dict.version, // 获取版本数据 getversionData: (state) => state.dict.versionData, // 获取终端Ip getTerminalIp: (state) => state.dict.terminalIp, // 获取操作员 getOperatorId: (state) => state.dict.operatorId, // 获取终端信息 getTerminal: (state) => state.dict.terminal, // 获取设备列表 getDeviceList: (state) => state.dict.deviceList, // 获取流程键 getFlowKey: (state) => state.dict.flowKey, // 获取流程信息 getFlow: (state) => state.dict.flow, // 获取链接流程键 getLinkFlowKey: (state) => state.dict.linkFlowKey, // 获取链接流程页 getLinkFlowPage: (state) => state.dict.linkFlowPage, // 获取业务流程 getBusinessTrace: (state) => state.dict.businessTrace, // 获取交易ID getTradeId: (state) => state.dict.tradeId, // 获取参数信息 getPage: (state) => state.dict.page, getCommon: (state) => state.dict.common, getDevice: (state) => state.dict.device, getSystem: (state) => state.dict.system, getStatic: (state) => state.dict.staticValue, getNull: (state) => state.dict.nullValue, getConfig: (state) => state.dict.config, getOut: (state) => state.dict.out, // 获取消息信息 getMsg: (state) => state.dict.msg, // 获取错误信息 getError: (state) => state.dict.error, // 获取语音信息 getVoice: (state) => state.dict.voice, // 获取凭条信息 getReceiptList: (state) => state.dict.receiptList, // 获取前台管理信息 getManage: (state) => state.dict.manage } })
二、调用示例
test-page-use-dict-store.vue <script setup lang="ts"> import { DICT_SYSTEM } from '@/const' import { useDataStore } from '@/store/useDictStore'const dataStore = useDataStore() dataStore.initDict() dataStore.setDictValue('version', '1.0.0') dataStore.syncData(DICT_SYSTEM, 'test', '123') </script>
<template> <div> <p> <button @click="dataStore.setDictValue('version', '1.0.0')"> setVersion('1.0.0') </button> <button @click="dataStore.setDictValue('version', '2.0.0')"> setVersion('2.0.0') </button> </p> <p>version: {{ dataStore.getVersion }}</p> <p>system: {{ dataStore.getSystem }}</p> </div> </template>
<style scoped></style>
三、运行测试
翻译
搜索
复制
<iframe></iframe> 标签:web,const,useDictStore,ssts,获取,state,dict,key,DICT From: https://www.cnblogs.com/lizhigang/p/18040901