import { createApp } from 'vue' import { createRouter, createWebHashHistory } from 'vue-router' import Vant from 'vant' import 'vant/lib/index.css' import App from './App.vue' import routes from './router/index.js' import AppIcon from './components/AppIcon.vue' import { hydrateSession, isLoggedIn } from './store/session.js' import './styles/theme.css' // 首屏同步恢复登录态,供路由守卫判断 hydrateSession() const router = createRouter({ history: createWebHashHistory(), routes, scrollBehavior() { return { top: 0 } } }) // 全局登录守卫:未登录跳登录页;已登录访问登录页则回首页 router.beforeEach((to) => { if (to.name !== 'login' && !isLoggedIn()) { return { name: 'login', query: to.fullPath && to.fullPath !== '/' ? { redirect: to.fullPath } : {} } } if (to.name === 'login' && isLoggedIn()) return '/home' }) const app = createApp(App) app.component('AppIcon', AppIcon) // 全局图标组件 app.use(router) app.use(Vant) app.mount('#app')