perf: 代码优化 store/modules/settings
This commit is contained in:
parent
7020a864f6
commit
32fae72f4f
@ -1,27 +1,20 @@
|
|||||||
import { ref } from "vue"
|
import { type Ref, ref } from "vue"
|
||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import layoutSettings from "@/config/layout"
|
import layoutSettings from "@/config/layout"
|
||||||
|
|
||||||
export const useSettingsStore = defineStore("settings", () => {
|
type SettingsStore = {
|
||||||
const fixedHeader = ref<boolean>(layoutSettings.fixedHeader)
|
// 使用映射类型来遍历 layoutSettings 对象的键
|
||||||
const showSettings = ref<boolean>(layoutSettings.showSettings)
|
[Key in keyof typeof layoutSettings]: Ref<(typeof layoutSettings)[Key]>
|
||||||
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)
|
|
||||||
|
|
||||||
return {
|
export const useSettingsStore = defineStore("settings", () => {
|
||||||
fixedHeader,
|
/** 状态对象 */
|
||||||
showSettings,
|
const state = {} as SettingsStore
|
||||||
showTagsView,
|
// 遍历 layoutSettings 对象的键值对
|
||||||
showSidebarLogo,
|
for (const [key, value] of Object.entries(layoutSettings)) {
|
||||||
showNotify,
|
// 使用类型断言来指定 key 的类型,将 value 包装在 ref 函数中,创建一个响应式变量
|
||||||
showThemeSwitch,
|
state[key as keyof SettingsStore] = ref(value)
|
||||||
showScreenfull,
|
|
||||||
showGreyMode,
|
|
||||||
showColorWeakness
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return state
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user