From 37af52a3a2e697edc9f6d971165a764e24871a17 Mon Sep 17 00:00:00 2001 From: Germey Date: Sun, 13 Jul 2025 18:22:26 +0800 Subject: [PATCH] Fix init theme failed error (#217) --- ...ud-nexior-d460bfbc-2dc4-404b-9135-1a2a803ce1d5.json | 7 +++++++ src/main.ts | 2 ++ src/utils/index.ts | 1 + src/utils/initializer.ts | 10 ++++++++++ src/utils/theme.ts | 7 +++++++ 5 files changed, 27 insertions(+) create mode 100644 change/@acedatacloud-nexior-d460bfbc-2dc4-404b-9135-1a2a803ce1d5.json create mode 100644 src/utils/theme.ts diff --git a/change/@acedatacloud-nexior-d460bfbc-2dc4-404b-9135-1a2a803ce1d5.json b/change/@acedatacloud-nexior-d460bfbc-2dc4-404b-9135-1a2a803ce1d5.json new file mode 100644 index 00000000..7f35d7fe --- /dev/null +++ b/change/@acedatacloud-nexior-d460bfbc-2dc4-404b-9135-1a2a803ce1d5.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix theme init failed issue", + "packageName": "@acedatacloud/nexior", + "email": "cqc@cuiqingcai.com", + "dependentChangeType": "patch" +} diff --git a/src/main.ts b/src/main.ts index 803e2856..68b75a29 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,6 +14,7 @@ import { initializeCookies, initializeDescription, initializeFavicon, + initializeTheme, initializeToken, initializeUser, initializeKeywords, @@ -35,6 +36,7 @@ const main = async () => { // non-async and no need to await initializeCurrency(); + initializeTheme(); initializeExchangeRate(); initializeTitle(); initializeDescription(); diff --git a/src/utils/index.ts b/src/utils/index.ts index 7060ba92..907127bf 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -7,3 +7,4 @@ export * from './login'; export * from './site'; export * from './price'; export * from './application'; +export * from './theme'; diff --git a/src/utils/initializer.ts b/src/utils/initializer.ts index 7da98bb4..fbbc3dc1 100644 --- a/src/utils/initializer.ts +++ b/src/utils/initializer.ts @@ -1,6 +1,7 @@ import { getCookie, setCookie } from 'typescript-cookie'; import favicon from '@/assets/images/favicon.ico'; import { getLocale } from '@/i18n'; +import { applyTheme } from './theme'; import store from '@/store'; import { IToken } from '@/models'; import psl from 'psl'; @@ -165,6 +166,15 @@ export const initializeToken = async () => { } }; +/** + * Initialize theme + */ +export const initializeTheme = async () => { + const theme = getCookie('THEME') || 'light'; + console.debug('initialize theme', theme); + applyTheme(theme); +}; + /** * Initialize user by token */ diff --git a/src/utils/theme.ts b/src/utils/theme.ts new file mode 100644 index 00000000..9a096a18 --- /dev/null +++ b/src/utils/theme.ts @@ -0,0 +1,7 @@ +export function applyTheme(theme: string) { + if (theme === 'dark') { + document.documentElement.classList.add('dark'); + } else { + document.documentElement.classList.remove('dark'); + } +}