# Common Rules & Best Practices ## File Organization ``` src/ ├── api/ # API request functions (axios) ├── components/ # Reusable Vue components ├── router/ # Vue Router configuration ├── stores/ # Pinia stores (one file per store) ├── styles/ # Global SCSS files ├── utils/ # Utility functions ├── views/ # Page components (routed) ├── App.vue # Root component └── main.js # Entry point ``` ## Vue 3 Composition API Rules ### Component Structure ```vue ``` ### Rules - ✅ Use ` ``` ### Rules - ✅ Centralize API calls in `api/` folder - ✅ Use request interceptors for auth tokens - ✅ Handle errors gracefully - ✅ Use async/await pattern - ✅ Return response.data directly from interceptor - ❌ Don't make API calls directly in components - ❌ Don't hardcode URLs ## Router Rules ### Route Definition ```javascript // router/index.js import { createRouter, createWebHistory } from 'vue-router' const routes = [ { path: '/', name: 'Home', component: () => import('@/views/Home.vue') }, { path: '/about', name: 'About', component: () => import('@/views/About.vue') }, { path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('@/views/NotFound.vue') } ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` ### Rules - ✅ Use lazy loading with `() => import()` - ✅ Use named routes - ✅ Use kebab-case for paths - ✅ Add 404 catch-all route - ✅ Use `createWebHistory()` for clean URLs - ❌ Don't use hash mode (#/) - ❌ Don't hardcode component imports ## Component Rules ### Naming - Files: PascalCase (e.g., `UserCard.vue`) - Components: PascalCase (e.g., ``) - Props: camelCase (e.g., `userName`) - Events: kebab-case (e.g., `@user-updated`) ### Props & Emits ```vue ``` ### Rules - ✅ Use `defineProps()` and `defineEmits()` - ✅ Always type props - ✅ Provide default values - ✅ Use descriptive prop names - ✅ Keep components focused (single responsibility) - ❌ Don't mutate props directly - ❌ Don't use `v-model` for complex logic ## Vant Component Rules ### Common Components - `` - Buttons - `` - List items - `` - Forms - `` - Form inputs - `` - Modals/Popups - `` - Tabs - `` - Infinite scroll lists - `` - Loading indicator - `` - Notifications ### Usage ```vue ``` ### Rules - ✅ Use Vant components for UI - ✅ Use Vant utilities (showToast, showDialog, etc.) - ✅ Customize with UnoCSS classes - ✅ Check Vant docs for component props - ❌ Don't create custom buttons/inputs (use Vant) - ❌ Don't override Vant styles globally ## Performance Rules ### Code Splitting - ✅ Use lazy loading for routes: `() => import('@/views/Page.vue')` - ✅ Use lazy loading for heavy components - ✅ Use `` for async components ### Optimization - ✅ Use `computed()` instead of methods for derived state - ✅ Use `v-show` for frequently toggled elements - ✅ Use `v-if` for rarely rendered elements - ✅ Use `key` binding for list items - ✅ Avoid unnecessary re-renders with `ref` and `reactive` ### Bundle Size - ✅ Tree-shake unused imports - ✅ Use dynamic imports for large libraries - ✅ Monitor bundle size with `vite build --analyze` ## Error Handling ### Try-Catch Pattern ```javascript async function fetchData() { try { const data = await api.getData() return data } catch (error) { console.error('Error fetching data:', error) showToast('Failed to load data') throw error } } ``` ### Rules - ✅ Always wrap async operations in try-catch - ✅ Show user-friendly error messages - ✅ Log errors for debugging - ✅ Handle network errors gracefully - ❌ Don't silently fail - ❌ Don't expose technical error details to users ## Testing Rules ### Unit Tests (if applicable) - Use Vitest for unit tests - Test stores, utilities, and API functions - Aim for 80%+ coverage on critical paths ### E2E Tests (if applicable) - Use Playwright or Cypress - Test user workflows - Test critical features ## Git & Commit Rules ### Commit Messages - Use conventional commits: `feat:`, `fix:`, `docs:`, `style:`, `refactor:`, `test:`, `chore:` - Example: `feat: add user authentication` - Keep messages concise and descriptive ### Branch Naming - Feature: `feature/user-auth` - Bug fix: `fix/login-error` - Docs: `docs/readme-update` ## Environment Variables ### .env Files ``` # .env (shared) VITE_API_BASE_URL=/api # .env.development (dev only) VITE_DEBUG=true # .env.production (prod only) VITE_DEBUG=false ``` ### Usage in Code ```javascript const apiUrl = import.meta.env.VITE_API_BASE_URL ``` ### Rules - ✅ Use `VITE_` prefix for client-side variables - ✅ Keep sensitive data in `.env.local` (not committed) - ✅ Document all env variables - ❌ Don't commit `.env.local` - ❌ Don't expose API keys in client code ## Accessibility Rules ### HTML Semantics - ✅ Use semantic HTML: `