vue3-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVue 3 Best Practices
Vue 3 最佳实践
Comprehensive performance optimization and development guide for Vue 3 applications. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
面向Vue 3应用的全面性能优化与开发指南。包含8个分类下的45条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Vue 3 components or composables
- Implementing reactive data and computed properties
- Reviewing code for performance issues
- Refactoring from Vue 2 to Vue 3
- Optimizing bundle size or load times
- Working with state management (Pinia/Vuex)
- Implementing async operations in components
在以下场景中可参考本指南:
- 编写新的Vue 3组件或可复用组合式函数(composables)
- 实现响应式数据与计算属性
- 评审代码以排查性能问题
- 从Vue 2迁移至Vue 3
- 优化包体积或加载时间
- 处理状态管理(Pinia/Vuex)相关工作
- 在组件中实现异步操作
Rule Categories by Priority
按优先级划分的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Reactivity Performance | CRITICAL | |
| 2 | Component Optimization | CRITICAL | |
| 3 | Bundle Size & Loading | HIGH | |
| 4 | Composition API | MEDIUM-HIGH | |
| 5 | Template Performance | MEDIUM | |
| 6 | State Management | MEDIUM | |
| 7 | Lifecycle Optimization | LOW-MEDIUM | |
| 8 | Advanced Patterns | LOW | |
| 优先级 | 分类 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 响应式性能 | 关键 | |
| 2 | 组件优化 | 关键 | |
| 3 | 包体积与加载 | 高 | |
| 4 | Composition API | 中高 | |
| 5 | 模板性能 | 中 | |
| 6 | 状态管理 | 中 | |
| 7 | 生命周期优化 | 中低 | |
| 8 | 高级模式 | 低 | |
Quick Reference
快速参考
1. Reactivity Performance (CRITICAL)
1. 响应式性能(关键)
- - Use ref for primitives, reactive for objects
reactivity-ref-vs-reactive - - Use shallowRef for large immutable objects
reactivity-shallow-ref - - Leverage computed property caching
reactivity-computed-caching - - Choose appropriate watcher
reactivity-watch-vs-watcheffect - - Minimize unref calls in hot paths
reactivity-unref-performance - - Use readonly for immutable data
reactivity-readonly-immutable
- - 基础类型使用ref,对象类型使用reactive
reactivity-ref-vs-reactive - - 大型不可变对象使用shallowRef
reactivity-shallow-ref - - 利用计算属性的缓存特性
reactivity-computed-caching - - 选择合适的监听器
reactivity-watch-vs-watcheffect - - 在高频路径中尽量减少unref调用
reactivity-unref-performance - - 不可变数据使用readonly
reactivity-readonly-immutable
2. Component Optimization (CRITICAL)
2. 组件优化(关键)
- - Use defineAsyncComponent for heavy components
component-async-components - - Use functional components for simple presentational logic
component-functional - - Cache expensive components with keep-alive
component-keep-alive - - Implement lazy hydration for non-critical components
component-lazy-hydration - - Use efficient prop validation
component-prop-validation - - Optimize event emissions
component-emit-performance
- - 大型组件使用defineAsyncComponent
component-async-components - - 简单展示型逻辑使用函数式组件
component-functional - - 使用keep-alive缓存开销较大的组件
component-keep-alive - - 对非关键组件实现懒水化
component-lazy-hydration - - 使用高效的属性校验
component-prop-validation - - 优化事件触发性能
component-emit-performance
3. Bundle Size & Loading (HIGH)
3. 包体积与加载(高)
- - Structure imports for optimal tree-shaking
bundle-tree-shaking - - Use dynamic imports for code splitting
bundle-dynamic-imports - - Use unplugin-auto-import for better DX
bundle-plugin-imports - - Import lodash functions individually
bundle-lodash-imports - - Use day.js instead of moment.js
bundle-moment-alternatives - - Optimize icon imports and usage
bundle-icons-optimization
- - 合理组织导入以实现最优摇树优化
bundle-tree-shaking - - 使用动态导入实现代码分割
bundle-dynamic-imports - - 使用unplugin-auto-import提升开发体验(DX)
bundle-plugin-imports - - 单独导入lodash函数
bundle-lodash-imports - - 使用day.js替代moment.js
bundle-moment-alternatives - - 优化图标导入与使用
bundle-icons-optimization
4. Composition API (MEDIUM-HIGH)
4. Composition API(中高)
- - Prefer <script setup> for better performance
composition-script-setup - - Extract reusable logic into composables
composition-composables-reuse - - Use provide/inject for dependency injection
composition-provide-inject - - Expose only necessary properties
composition-expose-selectively - - Use reactive transform where appropriate
composition-reactive-transform - - Configure auto-imports for better DX
composition-auto-import
- - 优先使用<script setup>以提升性能
composition-script-setup - - 将可复用逻辑提取为composables
composition-composables-reuse - - 使用provide/inject实现依赖注入
composition-provide-inject - - 仅暴露必要的属性
composition-expose-selectively - - 在合适场景下使用响应式转换
composition-reactive-transform - - 配置自动导入以提升开发体验(DX)
composition-auto-import
5. Template Performance (MEDIUM)
5. 模板性能(中)
- - Use v-once for static content
template-v-once - - Use v-memo for expensive list rendering
template-v-memo - - Optimize v-for keys for performance
template-key-optimization - - Choose v-if vs v-show appropriately
template-conditional-rendering - - Optimize slot usage and scoped slots
template-slot-performance - - Create efficient custom directives
template-directive-optimization
- - 静态内容使用v-once
template-v-once - - 开销较大的列表渲染使用v-memo
template-v-memo - - 优化v-for的key以提升性能
template-key-optimization - - 合理选择v-if与v-show
template-conditional-rendering - - 优化插槽与作用域插槽的使用
template-slot-performance - - 实现高效的自定义指令
template-directive-optimization
6. State Management (MEDIUM)
6. 状态管理(中)
- - Optimize Pinia store structure
state-pinia-optimization - - Use store composition patterns
state-store-composition - - Implement efficient state persistence
state-persistence - - Normalize complex state structures
state-normalization - - Optimize state subscriptions
state-subscription - - Configure devtools for debugging
state-devtools
- - 优化Pinia store结构
state-pinia-optimization - - 使用store组合模式
state-store-composition - - 实现高效的状态持久化
state-persistence - - 规范化复杂状态结构
state-normalization - - 优化状态订阅
state-subscription - - 配置开发者工具以调试
state-devtools
7. Lifecycle Optimization (LOW-MEDIUM)
7. 生命周期优化(中低)
- - Properly cleanup resources in onUnmounted
lifecycle-cleanup - - Handle async operations in setup
lifecycle-async-setup - - Clean up watchers and effects
lifecycle-watchers-cleanup - - Access DOM safely in lifecycle hooks
lifecycle-dom-access - - Handle SSR lifecycle differences
lifecycle-ssr-considerations
- - 在onUnmounted中正确清理资源
lifecycle-cleanup - - 在setup中处理异步操作
lifecycle-async-setup - - 清理监听器与副作用
lifecycle-watchers-cleanup - - 在生命周期钩子中安全访问DOM
lifecycle-dom-access - - 处理SSR生命周期差异
lifecycle-ssr-considerations
8. Advanced Patterns (LOW)
8. 高级模式(低)
- - Use Teleport for portal patterns
advanced-teleport-usage - - Implement Suspense with async components
advanced-suspense-async - - Create custom renderers when needed
advanced-custom-renderer - - Use compiler macros effectively
advanced-compiler-macros - - Develop efficient Vue plugins
advanced-plugin-development
- - 使用Teleport实现门户模式
advanced-teleport-usage - - 结合异步组件实现Suspense
advanced-suspense-async - - 在需要时创建自定义渲染器
advanced-custom-renderer - - 有效使用编译器宏
advanced-compiler-macros - - 开发高效的Vue插件
advanced-plugin-development
Framework Integration
框架集成
Vite Integration
Vite集成
- Utilize Vite's fast HMR and build optimizations
- Configure proper chunk splitting strategies
- Use Vite plugins for Vue-specific optimizations
- 利用Vite的快速热模块替换(HMR)与构建优化
- 配置合理的代码分割策略
- 使用针对Vue的Vite插件优化
TypeScript Integration
TypeScript集成
- Leverage Vue 3's improved TypeScript support
- Use proper type definitions for better DX
- Configure TypeScript for optimal build performance
- 利用Vue 3增强的TypeScript支持
- 使用正确的类型定义以提升开发体验(DX)
- 配置TypeScript以实现最优构建性能
Testing Integration
测试集成
- Use Vue Test Utils with Composition API
- Implement efficient component testing strategies
- Optimize test performance and reliability
- 结合Composition API使用Vue Test Utils
- 实现高效的组件测试策略
- 优化测试性能与可靠性
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/reactivity-ref-vs-reactive.md
rules/component-async-components.md
rules/composition-script-setup.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect Vue 3 code example with explanation
- Correct Vue 3 code example with explanation
- Performance impact and measurements
- Additional context and Vue 3-specific considerations
阅读单个规则文件以获取详细说明与代码示例:
rules/reactivity-ref-vs-reactive.md
rules/component-async-components.md
rules/composition-script-setup.md每个规则文件包含:
- 规则重要性的简要说明
- 错误的Vue 3代码示例及解释
- 正确的Vue 3代码示例及解释
- 性能影响与度量
- 额外背景信息及Vue 3特定注意事项
Migration from Vue 2
从Vue 2迁移
Special considerations for migrating from Vue 2:
- Composition API vs Options API patterns
- Reactivity system changes and optimizations
- Component definition and registration updates
- Event handling and lifecycle changes
从Vue 2迁移的特殊注意事项:
- Composition API与Options API模式对比
- 响应式系统的变更与优化
- 组件定义与注册的更新
- 事件处理与生命周期的变更
Performance Monitoring
性能监控
Tools and techniques for monitoring Vue 3 performance:
- Vue DevTools integration
- Performance profiling with browser tools
- Bundle analysis and optimization
- Runtime performance monitoring
用于监控Vue 3性能的工具与技术:
- Vue DevTools集成
- 使用浏览器工具进行性能分析
- 包分析与优化
- 运行时性能监控