main.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { createApp } from 'vue'
  2. import { createPinia } from 'pinia'
  3. import ElementPlus from 'element-plus'
  4. import * as ElementPlusIconsVue from '@element-plus/icons-vue'
  5. import 'element-plus/dist/index.css'
  6. import 'element-plus/theme-chalk/dark/css-vars.css'
  7. import App from './App.vue'
  8. import router from './router'
  9. import { useAuthStore } from '@/stores/auth'
  10. const app = createApp(App)
  11. // 注册Element Plus图标
  12. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  13. app.component(key, component)
  14. }
  15. const pinia = createPinia()
  16. app.use(pinia)
  17. app.use(router)
  18. app.use(ElementPlus)
  19. // 初始化认证状态
  20. const initAuth = async () => {
  21. const authStore = useAuthStore()
  22. // 如果有token但没有用户信息,尝试获取用户信息
  23. if (authStore.token && !authStore.user) {
  24. try {
  25. await authStore.checkAuth()
  26. } catch (error) {
  27. console.error('初始化认证状态失败:', error)
  28. }
  29. }
  30. }
  31. // 在应用挂载前初始化认证状态
  32. initAuth().then(() => {
  33. app.mount('#app')
  34. }).catch((error) => {
  35. console.error('应用初始化失败:', error)
  36. app.mount('#app')
  37. })