refactor: useTheme 中使用 initTheme 方法显式执行初始化 (#65)

This commit is contained in:
betterwusy 2023-03-28 17:48:39 +08:00 committed by GitHub
parent 6d0ef20b0b
commit 3b47cecfc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -4,8 +4,10 @@ import { useTheme } from "@/hooks/useTheme"
import { ElNotification } from "element-plus" import { ElNotification } from "element-plus"
import zhCn from "element-plus/lib/locale/lang/zh-cn" import zhCn from "element-plus/lib/locale/lang/zh-cn"
const { initTheme } = useTheme()
/** 初始化主题 */ /** 初始化主题 */
useTheme() initTheme()
/** 将 Element Plus 的语言设置为中文 */ /** 将 Element Plus 的语言设置为中文 */
const locale = zhCn const locale = zhCn

View File

@ -40,13 +40,15 @@ const setHtmlClassName = (value: ThemeName) => {
document.documentElement.className = value document.documentElement.className = value
} }
watchEffect(() => { const initTheme = () => {
const value = activeThemeName.value watchEffect(() => {
setHtmlClassName(value) const value = activeThemeName.value
setActiveThemeName(value) setHtmlClassName(value)
}) setActiveThemeName(value)
})
}
/** 主题 hook */ /** 主题 hook */
export function useTheme() { export function useTheme() {
return { themeList, activeThemeName, setTheme } return { themeList, activeThemeName, initTheme, setTheme }
} }