---
description: Vue.js best practices and patterns for modern web applications
globs: **/*.vue, **/*.ts, components/**/*
---
# Vue.js Best Practices
## Component Structure
- Use Composition API over Options API
- Keep components small and focused
- Use proper TypeScript integration
- Implement proper props validation
- Use proper emit declarations
- Keep template logic minimal
## Composition API
- Use proper ref and reactive
- Implement proper lifecycle hooks
- Use composables for reusable logic
- Keep setup function clean
- Use proper computed properties
- Implement proper watchers
## State Management
- Use Pinia for state management
- Keep stores modular
- Use proper state composition
- Implement proper actions
- Use proper getters
- Handle async state properly
## Performance
- Use proper component lazy loading
- Implement proper caching
- Use proper computed properties
- Avoid unnecessary watchers
- Use proper v-show vs v-if
- Implement proper key management
## Routing
- Use Vue Router properly
- Implement proper navigation guards
- Use proper route meta fields
- Handle route params properly
- Implement proper lazy loading
- Use proper navigation methods
## Forms
- Use v-model properly
- Implement proper validation
- Handle form submission properly
- Show proper loading states
- Use proper error handling
- Implement proper form reset
## TypeScript Integration
- Use proper component type definitions
- Implement proper prop types
- Use proper emit declarations
- Handle proper type inference
- Use proper composable types
- Implement proper store types
## Testing
- Write proper unit tests
- Implement proper component tests
- Use Vue Test Utils properly
- Test composables properly
- Implement proper mocking
- Test async operations
## Best Practices
- Follow Vue style guide
- Use proper naming conventions
- Keep components organized
- Implement proper error handling
- Use proper event handling
- Document complex logic
## Build and Tooling
- Use Vite for development
- Configure proper build setup
- Use proper environment variables
- Implement proper code splitting
- Use proper asset handling
- Configure proper optimization
Vue.js 最佳实践
描述
Vue.js 现代 Web 应用程序的最佳实践和模式
适用范围
/.vue, /.ts, components//* 目录下的所有文件
组件结构
- 使用组合式 API 而不是选项式 API
- 保持组件小巧且专注
- 使用适当的 TypeScript 集成
- 实现适当的 props 验证
- 使用适当的 emit 声明
- 保持模板逻辑最小化
组合式 API
- 正确使用 ref 和 reactive
- 实现适当的生命周期钩子
- 使用组合式函数实现可重用逻辑
- 保持 setup 函数整洁
- 使用适当的计算属性
- 实现适当的侦听器
状态管理
- 使用 Pinia 进行状态管理
- 保持 store 模块化
- 使用适当的状态组合
- 实现适当的 actions
- 使用适当的 getters
- 正确处理异步状态
性能
- 使用适当的组件懒加载
- 实现适当的缓存
- 使用适当的计算属性
- 避免不必要的侦听器
- 正确使用 v-show 和 v-if
- 实现适当的 key 管理
路由
- 正确使用 Vue Router
- 实现适当的导航守卫
- 使用适当的路由元字段
- 正确处理路由参数
- 实现适当的懒加载
- 使用适当的导航方法
表单
- 正确使用 v-model
- 实现适当的验证
- 正确处理表单提交
- 显示适当的加载状态
- 使用适当的错误处理
- 实现适当的表单重置
TypeScript 集成
- 使用适当的组件类型定义
- 实现适当的 prop 类型
- 使用适当的 emit 声明
- 处理适当的类型推断
- 使用适当的组合式函数类型
- 实现适当的 store 类型
测试
- 编写适当的单元测试
- 实现适当的组件测试
- 正确使用 Vue Test Utils
- 正确测试组合式函数
- 实现适当的模拟
- 测试异步操作
最佳实践
- 遵循 Vue 风格指南
- 使用适当的命名约定
- 保持组件组织
- 实现适当的错误处理
- 使用适当的事件处理
- 记录复杂逻辑
构建和工具
- 使用 Vite 进行开发
- 配置适当的构建设置
- 使用适当的环境变量
- 实现适当的代码分割
- 使用适当的资源处理
- 配置适当的优化