|
@@ -22,6 +22,49 @@ import router from './router'
|
|
|
|
|
|
|
|
const app = createApp(App)
|
|
const app = createApp(App)
|
|
|
|
|
|
|
|
|
|
+// 动态加载暗黑主题
|
|
|
|
|
+async function loadDarkTheme() {
|
|
|
|
|
+ // const linkEl = document.createElement('link')
|
|
|
|
|
+ // linkEl.rel = 'stylesheet'
|
|
|
|
|
+ // linkEl.href = 'https://unpkg.com/element-plus/theme-chalk/dark/css-vars.css' // 暗黑主题 CSS
|
|
|
|
|
+ // linkEl.id = 'dark-theme-style'
|
|
|
|
|
+ // document.head.appendChild(linkEl)
|
|
|
|
|
+
|
|
|
|
|
+ await import('element-plus/theme-chalk/dark/css-vars.css') // 动态导入暗黑主题
|
|
|
|
|
+}
|
|
|
|
|
+// 动态移除暗黑主题
|
|
|
|
|
+function unloadDarkTheme() {
|
|
|
|
|
+ const darkThemeStylesheet = document.getElementById('dark-theme-style')
|
|
|
|
|
+ if (darkThemeStylesheet) {
|
|
|
|
|
+ darkThemeStylesheet.remove()
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+// 根据用户偏好或系统设置决定是否加载暗黑主题
|
|
|
|
|
+if (
|
|
|
|
|
+ (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) ||
|
|
|
|
|
+ localStorage.getItem('theme') === 'dark'
|
|
|
|
|
+) {
|
|
|
|
|
+ loadDarkTheme()
|
|
|
|
|
+ document.documentElement.classList.add('dark')
|
|
|
|
|
+}
|
|
|
|
|
+// 根据用户偏好或系统设置决定是否添加暗黑模式类名
|
|
|
|
|
+if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
|
|
|
+ document.documentElement.classList.add('dark')
|
|
|
|
|
+}
|
|
|
|
|
+// 提供一个全局方法来切换主题
|
|
|
|
|
+app.config.globalProperties.$toggleDarkMode = () => {
|
|
|
|
|
+ const html = document.documentElement
|
|
|
|
|
+ if (html.classList.contains('dark')) {
|
|
|
|
|
+ unloadDarkTheme()
|
|
|
|
|
+ html.classList.remove('dark')
|
|
|
|
|
+ localStorage.setItem('theme', 'light')
|
|
|
|
|
+ } else {
|
|
|
|
|
+ loadDarkTheme()
|
|
|
|
|
+ html.classList.add('dark')
|
|
|
|
|
+ localStorage.setItem('theme', 'dark')
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
|
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
|
|
app.component(key, component)
|
|
app.component(key, component)
|
|
|
}
|
|
}
|