From 3b47cecfc96eae0a43da6a87a1e992cfb6e22570 Mon Sep 17 00:00:00 2001 From: betterwusy <75969666+betterwusy@users.noreply.github.com> Date: Tue, 28 Mar 2023 17:48:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20useTheme=20=E4=B8=AD=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20initTheme=20=E6=96=B9=E6=B3=95=E6=98=BE=E5=BC=8F?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=88=9D=E5=A7=8B=E5=8C=96=20(#65)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 4 +++- src/hooks/useTheme.ts | 14 ++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/App.vue b/src/App.vue index da4ac7d..1df0ab9 100644 --- a/src/App.vue +++ b/src/App.vue @@ -4,8 +4,10 @@ import { useTheme } from "@/hooks/useTheme" import { ElNotification } from "element-plus" import zhCn from "element-plus/lib/locale/lang/zh-cn" +const { initTheme } = useTheme() + /** 初始化主题 */ -useTheme() +initTheme() /** 将 Element Plus 的语言设置为中文 */ const locale = zhCn diff --git a/src/hooks/useTheme.ts b/src/hooks/useTheme.ts index 636231e..d0c8e27 100644 --- a/src/hooks/useTheme.ts +++ b/src/hooks/useTheme.ts @@ -40,13 +40,15 @@ const setHtmlClassName = (value: ThemeName) => { document.documentElement.className = value } -watchEffect(() => { - const value = activeThemeName.value - setHtmlClassName(value) - setActiveThemeName(value) -}) +const initTheme = () => { + watchEffect(() => { + const value = activeThemeName.value + setHtmlClassName(value) + setActiveThemeName(value) + }) +} /** 主题 hook */ export function useTheme() { - return { themeList, activeThemeName, setTheme } + return { themeList, activeThemeName, initTheme, setTheme } }