import { createRouter, createWebHistory } from 'vue-router' const router = createRouter({ history: createWebHistory(), routes: [ { path: '/', name: 'Home', component: () => import('@/views/Home.vue'), meta: { title: '首页', requiresAuth: true } }, { path: '/expense/add', name: 'ExpenseAdd', component: () => import('@/views/expense/Add.vue'), meta: { title: '新增支出', requiresAuth: true } }, { path: '/receivable', name: 'ReceivableList', component: () => import('@/views/receivable/List.vue'), meta: { title: '应收款列表', requiresAuth: true } }, { path: '/project', name: 'ProjectList', component: () => import('@/views/project/List.vue'), meta: { title: '项目列表', requiresAuth: true } }, { path: '/customer', name: 'CustomerList', component: () => import('@/views/customer/List.vue'), meta: { title: '客户列表', requiresAuth: true } }, { path: '/my', name: 'My', component: () => import('@/views/my/Index.vue'), meta: { title: '我的', requiresAuth: true } }, { path: '/login', name: 'Login', component: () => import('@/views/Login.vue'), meta: { title: '登录', requiresAuth: false } } ] }) // 路由守卫 router.beforeEach((to, _from, next) => { // 设置页面标题 if (to.meta.title) { document.title = to.meta.title as string } // 检查是否需要登录 if (to.meta.requiresAuth) { const token = localStorage.getItem('token') if (!token) { next('/login') return } } // 已登录用户不能访问登录页 if (to.path === '/login') { const token = localStorage.getItem('token') if (token) { next('/') return } } next() }) export default router