Vue.js Best Practices
Code organization
1. Use composition API
2. Component naming
3. File structure
Component design
1. Single responsibility principle
2. Props verification
3. Event naming
Responsive data
1. Use ref and reactive
2. Avoid directly modifying props
Computed properties and listeners
1. Computed properties should be pure functions
2. Avoid modifying the listened value in the listener
Template Best Practices
1. Provide key when using v-for
2. Avoid using v-if and v-for at the same time
3. Use concise template expressions
Performance optimization
1. Use v-show vs v-if
2. Lazy loading of components
3. Use v-memo
Combined functions
1. Naming Convention
2. Return responsive object
Error handling
1. Global error handling
2. Component error boundary
TypeScript Best Practices
1. Define types for props
2. Define the type for emits
Styling Best Practices
1. Use scoped style
2. Use CSS variables
3. Avoid depth selector abuse
Testing Best Practices
1. Test user behavior
2. Use test ID
Security Best Practices
1. Avoid XSS attacks
2. Environment variable security
Code review checklist
Components
- ✅ Component names use PascalCase
- ✅ Props are fully typed and verified
- ✅ Events are named using kebab-case
- ✅ Components have single responsibility
- ✅ Use scoped style
Performance
- ✅ Use lazy loading for large components
- ✅ List uses unique key
- ✅ Avoid using v-if and v-for at the same time
- ✅ Use computed attribute cache appropriately
- ✅ Images use lazy loading
Code quality
- ✅ No console.log
- ✅ No unused variables
- ✅ Uniform code format
- ✅ There are necessary comments
- ✅ Passed ESLint check
Test
- ✅ Key functions are covered by tests
- ✅ Test user behavior rather than implementation
- ✅ Test edge cases
- ✅ Mock external dependencies
Summarize
- Use combined APIs and
<script setup> - Follow naming conventions and file structure
- Props validation and type definition
- Proper use of computed properties and listeners
- Pay attention to performance optimization -Write testable code
- Pay attention to safety issues
- Keep code clean and maintainable
Next step
Finally, learn Learning Resources to learn more about Vue learning resources.