# 02-路由和布局设计 ## 目标 配置 Vue Router 路由系统,实现页面布局和导航。 --- ## 前置要求 - 项目结构已初始化 - Vue Router 已安装 - 模拟数据服务已创建 --- ## 实施步骤 ### 步骤 1:创建路由配置 创建 `src/router/index.ts`: ```typescript import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' const router = createRouter({ history: createWebHistory(), routes: [ { path: '/login', name: 'Login', component: () => import('@/views/auth/LoginView.vue'), meta: { requiresAuth: false } }, { path: '/', component: () => import('@/views/layout/MainLayout.vue'), meta: { requiresAuth: true }, children: [ { path: '', name: 'Home', component: () => import('@/views/home/HomeView.vue') }, { path: 'chat', name: 'ChatList', component: () => import('@/views/chat/ChatListView.vue') }, { path: 'chat/:id', name: 'ChatDetail', component: () => import('@/views/chat/ChatDetailView.vue') }, { path: 'constitution', name: 'Constitution', component: () => import('@/views/constitution/ConstitutionView.vue') }, { path: 'constitution/test', name: 'ConstitutionTest', component: () => import('@/views/constitution/ConstitutionTestView.vue') }, { path: 'constitution/result', name: 'ConstitutionResult', component: () => import('@/views/constitution/ConstitutionResultView.vue') }, { path: 'profile', name: 'Profile', component: () => import('@/views/profile/ProfileView.vue') }, { path: 'profile/health-record', name: 'HealthRecord', component: () => import('@/views/profile/HealthRecordView.vue') } ] } ] }) // 路由守卫 router.beforeEach((to, from, next) => { const authStore = useAuthStore() if (to.meta.requiresAuth && !authStore.isLoggedIn) { next('/login') } else if (to.path === '/login' && authStore.isLoggedIn) { next('/') } else { next() } }) export default router ``` ### 步骤 2:创建认证状态 Store 创建 `src/stores/auth.ts`: ```typescript import { defineStore } from 'pinia' import { ref, computed } from 'vue' import type { User } from '@/types' export const useAuthStore = defineStore('auth', () => { const user = ref(null) const token = ref(localStorage.getItem('token')) const isLoggedIn = computed(() => !!token.value) function login(userData: User) { user.value = userData token.value = 'mock-token-' + userData.id localStorage.setItem('token', token.value) localStorage.setItem('user', JSON.stringify(userData)) } function logout() { user.value = null token.value = null localStorage.removeItem('token') localStorage.removeItem('user') } // 初始化时从 localStorage 恢复用户信息 function init() { const savedUser = localStorage.getItem('user') if (savedUser && token.value) { user.value = JSON.parse(savedUser) } } init() return { user, token, isLoggedIn, login, logout } }) ``` ### 步骤 3:创建主布局 创建 `src/views/layout/MainLayout.vue`: ```vue ``` ### 步骤 4:更新 App.vue ```vue ``` --- ## 路由结构 ``` /login - 登录页 / - 主布局 ├── / - 首页 ├── /chat - 对话列表 ├── /chat/:id - 对话详情 ├── /constitution - 体质分析首页 ├── /constitution/test - 体质问卷 ├── /constitution/result - 体质结果 ├── /profile - 个人中心 └── /profile/health-record - 健康档案 ``` --- ## 验收标准 - [ ] 路由配置正确 - [ ] 布局显示正常 - [ ] 导航切换正常 - [ ] 登录状态守卫正常 --- ## 预计耗时 25-30 分钟 --- ## 下一步 完成后进入 `03-Web原型开发/03-登录页面.md`