From 9836d6d9bde266c1e79333d505e1f218567e7ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=88=E5=A4=9C?= <768242801@qq.com> Date: Mon, 11 Nov 2024 15:10:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E4=BD=BF=E7=94=A8Map=E4=BB=A3=E6=9B=BFArray?= =?UTF-8?q?=EF=BC=8C=E6=9B=B4=E9=AB=98=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/modules/dict.ts | 51 +++++++++++++++------------------------ 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/src/store/modules/dict.ts b/src/store/modules/dict.ts index 2f937b9d..cd1a41d3 100644 --- a/src/store/modules/dict.ts +++ b/src/store/modules/dict.ts @@ -1,29 +1,15 @@ export const useDictStore = defineStore('dict', () => { - const dict = ref< - Array<{ - key: string; - value: DictDataOption[]; - }> - >([]); + const dict = ref>(new Map()); /** * 获取字典 * @param _key 字典key */ const getDict = (_key: string): DictDataOption[] | null => { - if (_key == null && _key == '') { + if (!_key) { return null; } - try { - for (let i = 0; i < dict.value.length; i++) { - if (dict.value[i].key == _key) { - return dict.value[i].value; - } - } - } catch (e) { - return null; - } - return null; + return dict.value.get(_key) || null; }; /** @@ -32,11 +18,15 @@ export const useDictStore = defineStore('dict', () => { * @param _value 字典value */ const setDict = (_key: string, _value: DictDataOption[]) => { - if (_key !== null && _key !== '') { - dict.value.push({ - key: _key, - value: _value - }); + if (!_key) { + return false; + } + try { + dict.value.set(_key, _value); + return true; + } catch (e) { + console.error('Error in setDict:', e); + return false; } }; @@ -45,25 +35,22 @@ export const useDictStore = defineStore('dict', () => { * @param _key */ const removeDict = (_key: string): boolean => { - let bln = false; + if (!_key) { + return false; + } try { - for (let i = 0; i < dict.value.length; i++) { - if (dict.value[i].key == _key) { - dict.value.splice(i, 1); - return true; - } - } + return dict.value.delete(_key); } catch (e) { - bln = false; + console.error('Error in removeDict:', e); + return false; } - return bln; }; /** * 清空字典 */ const cleanDict = (): void => { - dict.value = []; + dict.value.clear(); }; return { -- Gitee