From 9fd306227eb71a3ee000ca824d3561436bbc2135 Mon Sep 17 00:00:00 2001 From: wf <2547096351@qq.com> Date: Mon, 29 Apr 2024 11:58:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20pinia=E7=9A=84=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/config/index.ts | 23 ++++++++++------------- src/store/index.ts | 1 + src/store/modules/theme-config.ts | 3 ++- src/store/modules/user-info.ts | 3 ++- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/store/config/index.ts b/src/store/config/index.ts index c7cac3e..ab7eaea 100644 --- a/src/store/config/index.ts +++ b/src/store/config/index.ts @@ -1,19 +1,16 @@ -import { PersistedStateOptions } from "pinia-plugin-persistedstate"; - /** - * @description pinia 持久化参数配置 - * @param {String} key 存储到持久化的 name - * @param {Array} paths 需要持久化的 state name - * @return persist - * */ -const piniaPersistConfig = (key: string, paths?: string[]) => { - const persist: PersistedStateOptions = { + * https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/config.html + * @description: 持久化配置 + * @param {string} key 存储的key + * @param {Array} paths 需要持久化的字段 + * @return {*} + */ +const persistedstateConfig = (key: string, paths?: Array) => { + return { key, - storage: localStorage, - // storage: sessionStorage, + storage: localStorage, // 存储方式,使用localStorage,也可以使用sessionStorage paths }; - return persist; }; -export default piniaPersistConfig; +export default persistedstateConfig; diff --git a/src/store/index.ts b/src/store/index.ts index 85f5b14..41b0b65 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,4 +1,5 @@ // https://pinia.vuejs.org/zh/ +// https://prazdevs.github.io/pinia-plugin-persistedstate/zh/ // store/index.js import { createPinia } from "pinia"; import piniaPluginPersistedstate from "pinia-plugin-persistedstate"; diff --git a/src/store/modules/theme-config.ts b/src/store/modules/theme-config.ts index df014b8..77f4636 100644 --- a/src/store/modules/theme-config.ts +++ b/src/store/modules/theme-config.ts @@ -1,4 +1,5 @@ import { defineStore } from "pinia"; +import persistedstateConfig from "@/store/config/index"; /** * 全局配置 * @methods setCollapsed 设置菜单折叠 @@ -25,5 +26,5 @@ export const useThemeConfig = defineStore("theme-config", { this.language = data; } }, - persist: true + persist: persistedstateConfig("theme-config") }); diff --git a/src/store/modules/user-info.ts b/src/store/modules/user-info.ts index 0417697..85879a3 100644 --- a/src/store/modules/user-info.ts +++ b/src/store/modules/user-info.ts @@ -1,4 +1,5 @@ import { defineStore } from "pinia"; +import persistedstateConfig from "@/store/config/index"; /** * 用户信息 * @methods setAccount 设置账号信息 @@ -25,5 +26,5 @@ export const useUserInfoStore = defineStore("user-info", { this.token = ""; } }, - persist: true + persist: persistedstateConfig("user-info") });