import { createRouter, createWebHistory } from 'vue-router'; const routes = [ { path: '/', redirect: '/posts' }, { path: '/login', name: 'login', component: () => import('./views/Login.vue'), meta: { guest: true } }, { path: '/posts', name: 'posts', component: () => import('./views/Posts.vue') }, { path: '/notices', name: 'notices', component: () => import('./views/Notices.vue') }, { path: '/secondhand', name: 'secondhand', component: () => import('./views/Secondhand.vue') }, { path: '/housekeeping', name: 'housekeeping', component: () => import('./views/Housekeeping.vue') }, { path: '/chat', name: 'chat', component: () => import('./views/ChatList.vue') }, { path: '/chat/:id', name: 'chat-detail', component: () => import('./views/ChatDetail.vue') }, { path: '/live', name: 'live', component: () => import('./views/LiveList.vue') }, { path: '/live/host/:id', name: 'live-host', component: () => import('./views/LiveHost.vue') }, { path: '/live/watch/:id', name: 'live-watch', component: () => import('./views/LiveWatch.vue') }, { path: '/profile', name: 'profile', component: () => import('./views/Profile.vue') } ]; const router = createRouter({ history: createWebHistory(), routes }); // 路由守卫:未登录跳转登录页 router.beforeEach((to) => { const token = localStorage.getItem('token'); if (!token && !to.meta.guest) { return { name: 'login' }; } if (token && to.meta.guest) { return { name: 'posts' }; } }); export default router;