main.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import './assets/main.css'
  2. import './styles/index.scss'
  3. import './styles/icons/iconfont.js'
  4. import VXETable from 'vxe-table'
  5. import 'vxe-table/lib/style.css'
  6. import enUS from 'vxe-table/lib/locale/lang/en-US'
  7. import VxeUI from 'vxe-pc-ui'
  8. import 'element-plus/dist/index.css'
  9. import 'vxe-pc-ui/lib/style.css'
  10. import Antd from 'ant-design-vue'
  11. import 'ant-design-vue/dist/reset.css'
  12. import * as ElementPlusIconsVue from '@element-plus/icons-vue'
  13. import VXETablePluginExportXLSX from 'vxe-table-plugin-export-xlsx'
  14. import ExcelJS from 'exceljs'
  15. import { VLoading } from './directive/VLoading'
  16. import { createApp } from 'vue'
  17. import { createPinia } from 'pinia'
  18. import App from './App.vue'
  19. import router from './router'
  20. const app = createApp(App)
  21. // 动态加载暗黑主题
  22. async function loadDarkTheme() {
  23. await import('element-plus/theme-chalk/dark/css-vars.css') // 动态导入暗黑主题
  24. }
  25. // 动态移除暗黑主题
  26. function unloadDarkTheme() {
  27. const darkThemeStylesheet = document.getElementById('dark-theme-style')
  28. if (darkThemeStylesheet) {
  29. darkThemeStylesheet.remove()
  30. }
  31. }
  32. // 根据用户偏好或系统设置决定是否加载暗黑主题 (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) ||
  33. if (localStorage.getItem('theme') === 'dark') {
  34. loadDarkTheme()
  35. document.documentElement.classList.add('dark')
  36. }
  37. // 根据用户偏好或系统设置决定是否添加暗黑模式类名
  38. // if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
  39. // document.documentElement.classList.add('dark')
  40. // }
  41. // 提供一个全局方法来切换主题
  42. app.config.globalProperties.$toggleDarkMode = () => {
  43. const html = document.documentElement
  44. if (html.classList.contains('dark')) {
  45. unloadDarkTheme()
  46. html.classList.remove('dark')
  47. localStorage.setItem('theme', 'light')
  48. } else {
  49. loadDarkTheme()
  50. html.classList.add('dark')
  51. localStorage.setItem('theme', 'dark')
  52. }
  53. }
  54. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  55. app.component(key, component)
  56. }
  57. // 让VxeTable支持导出Excel
  58. VXETable.use(VXETablePluginExportXLSX, {
  59. ExcelJS
  60. })
  61. VXETable.setI18n('en-US', enUS)
  62. VXETable.setLanguage('en-US')
  63. app.use(createPinia())
  64. app.use(VXETable)
  65. app.use(VxeUI)
  66. app.use(router)
  67. app.use(Antd)
  68. // 注册全局指令
  69. app.directive('vloading', VLoading)
  70. app.mount('#app')