diff --git a/src/store/modules/settings.ts b/src/store/modules/settings.ts index 8ce2138..fc25d1e 100644 --- a/src/store/modules/settings.ts +++ b/src/store/modules/settings.ts @@ -1,27 +1,20 @@ -import { ref } from "vue" +import { type Ref, ref } from "vue" import { defineStore } from "pinia" import layoutSettings from "@/config/layout" -export const useSettingsStore = defineStore("settings", () => { - const fixedHeader = ref(layoutSettings.fixedHeader) - const showSettings = ref(layoutSettings.showSettings) - const showTagsView = ref(layoutSettings.showTagsView) - const showSidebarLogo = ref(layoutSettings.showSidebarLogo) - const showNotify = ref(layoutSettings.showNotify) - const showThemeSwitch = ref(layoutSettings.showThemeSwitch) - const showScreenfull = ref(layoutSettings.showScreenfull) - const showGreyMode = ref(layoutSettings.showGreyMode) - const showColorWeakness = ref(layoutSettings.showColorWeakness) +type SettingsStore = { + // 使用映射类型来遍历 layoutSettings 对象的键 + [Key in keyof typeof layoutSettings]: Ref<(typeof layoutSettings)[Key]> +} - return { - fixedHeader, - showSettings, - showTagsView, - showSidebarLogo, - showNotify, - showThemeSwitch, - showScreenfull, - showGreyMode, - showColorWeakness +export const useSettingsStore = defineStore("settings", () => { + /** 状态对象 */ + const state = {} as SettingsStore + // 遍历 layoutSettings 对象的键值对 + for (const [key, value] of Object.entries(layoutSettings)) { + // 使用类型断言来指定 key 的类型,将 value 包装在 ref 函数中,创建一个响应式变量 + state[key as keyof SettingsStore] = ref(value) } + + return state })