import { createRouter, createWebHistory } from 'vue-router' import Login from '../pages/Login.vue' import Dashboard from '../pages/Dashboard.vue' import Device from '../pages/Device.vue' import FeedPlan from '../pages/FeedPlan.vue' import FeedPlanDetail from '../pages/FeedPlanDetail.vue' const routes = [ { path: '/', name: 'Login', component: Login }, { path: '/dashboard', name: 'Dashboard', component: Dashboard, meta: { requiresAuth: true } }, { path: '/device', name: 'Device', component: Device, meta: { requiresAuth: true } }, { path: '/feed/plan', name: 'FeedPlan', component: FeedPlan, meta: { requiresAuth: true } }, { path: '/feed/plan/detail/:id', name: 'FeedPlanDetail', component: FeedPlanDetail, meta: { requiresAuth: true }, props: true } ] const router = createRouter({ history: createWebHistory(), routes }) // 路由守卫 router.beforeEach((to, from, next) => { const token = localStorage.getItem('authToken') if (to.matched.some(record => record.meta.requiresAuth)) { if (!token) { next('/') } else { next() } } else { if (token && to.name === 'Login') { next('/dashboard') } else { next() } } }) export default router