Template
1
0
mirror of https://github.com/un-pany/v3-admin-vite.git synced 2025-04-21 11:29:20 +08:00

perf: 代码优化 store/modules/settings

This commit is contained in:
pany 2023-05-26 13:04:12 +08:00
parent 7020a864f6
commit 32fae72f4f

View File

@ -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<boolean>(layoutSettings.fixedHeader)
const showSettings = ref<boolean>(layoutSettings.showSettings)
const showTagsView = ref<boolean>(layoutSettings.showTagsView)
const showSidebarLogo = ref<boolean>(layoutSettings.showSidebarLogo)
const showNotify = ref<boolean>(layoutSettings.showNotify)
const showThemeSwitch = ref<boolean>(layoutSettings.showThemeSwitch)
const showScreenfull = ref<boolean>(layoutSettings.showScreenfull)
const showGreyMode = ref<boolean>(layoutSettings.showGreyMode)
const showColorWeakness = ref<boolean>(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
})