vue-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVue Best Practices
Vue最佳实践
Comprehensive performance optimization guide for Vue.js applications. Contains 40+ rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
Vue.js应用程序综合性能优化指南。包含8个类别下的40+条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Vue components
- Implementing reactive state and computed properties
- Reviewing code for performance issues
- Refactoring existing Vue code
- Optimizing rendering and re-renders
- Working with Composition API or Options API
在以下场景中参考本指南:
- 编写新的Vue组件
- 实现响应式状态与计算属性
- 审查代码以排查性能问题
- 重构现有Vue代码
- 优化渲染与重渲染
- 使用Composition API或Options API
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Reactivity Fundamentals | CRITICAL | |
| 2 | Component Performance | CRITICAL | |
| 3 | Computed & Watchers | HIGH | |
| 4 | Template Optimization | MEDIUM-HIGH | |
| 5 | Composition API Patterns | MEDIUM | |
| 6 | State Management | MEDIUM | |
| 7 | Async & Data Fetching | LOW-MEDIUM | |
| 8 | Advanced Patterns | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 响应式基础 | 关键 | |
| 2 | 组件性能 | 关键 | |
| 3 | 计算属性与监听器 | 高 | |
| 4 | 模板优化 | 中高 | |
| 5 | Composition API 模式 | 中 | |
| 6 | 状态管理 | 中 | |
| 7 | 异步与数据获取 | 中低 | |
| 8 | 高级模式 | 低 | |
Quick Reference
快速参考
1. Reactivity Fundamentals (CRITICAL)
1. 响应式基础(关键)
- - Use ref() for primitives, reactive() for objects
reactivity-ref-vs-reactive - - Don't destructure reactive objects
reactivity-avoid-destructure - - Use toRefs() when destructuring is needed
reactivity-toRefs - - Use shallowRef() for large non-reactive data
reactivity-shallowRef - - Use toRaw() for read-only operations on large data
reactivity-raw-values
- - 对基本类型使用ref(),对对象使用reactive()
reactivity-ref-vs-reactive - - 不要解构响应式对象
reactivity-avoid-destructure - - 当需要解构时使用toRefs()
reactivity-toRefs - - 对大型非响应式数据使用shallowRef()
reactivity-shallowRef - - 对大型数据执行只读操作时使用toRaw()
reactivity-raw-values
2. Component Performance (CRITICAL)
2. 组件性能(关键)
- - Use v-once for static content
component-v-once - - Use v-memo for expensive list items
component-v-memo - - Use defineAsyncComponent for heavy components
component-async - - Cache component state with KeepAlive
component-keep-alive - - Prefer functional components for stateless UI
component-functional
- - 对静态内容使用v-once
component-v-once - - 对开销大的列表项使用v-memo
component-v-memo - - 对重型组件使用defineAsyncComponent
component-async - - 使用KeepAlive缓存组件状态
component-keep-alive - - 无状态UI优先使用函数式组件
component-functional
3. Computed & Watchers (HIGH)
3. 计算属性与监听器(高)
- - Use computed() for derived values, not methods
computed-cache - - Avoid setters in computed when possible
computed-getter-only - - Minimize computed dependencies
computed-dependencies - - Avoid immediate watchers, use computed instead
watch-immediate - - Avoid deep watchers on large objects
watch-deep-avoid - - Always cleanup async watchers
watch-cleanup
- - 对派生值使用computed(),而非方法
computed-cache - - 尽可能避免在computed中使用setter
computed-getter-only - - 最小化computed的依赖项
computed-dependencies - - 避免使用immediate监听器,改用computed
watch-immediate - - 避免对大型对象使用深度监听器
watch-deep-avoid - - 始终清理异步监听器
watch-cleanup
4. Template Optimization (MEDIUM-HIGH)
4. 模板优化(中高)
- - v-show for frequent toggles, v-if for rare
template-v-show-vs-if - - Always use unique keys in v-for
template-key-attribute - - Never use v-if and v-for on same element
template-avoid-v-if-v-for - - Let compiler hoist static content
template-static-hoisting - - Use event modifiers instead of JS handlers
template-event-modifiers
- - 频繁切换用v-show,极少切换用v-if
template-v-show-vs-if - - 在v-for中始终使用唯一key
template-key-attribute - - 永远不要在同一元素上同时使用v-if和v-for
template-avoid-v-if-v-for - - 让编译器提升静态内容
template-static-hoisting - - 使用事件修饰符替代JS处理函数
template-event-modifiers
5. Composition API Patterns (MEDIUM)
5. Composition API 模式(中)
- - One concern per composable
composable-single-responsibility - - Return refs, not reactive objects
composable-return-refs - - Handle cleanup in composables
composable-cleanup - - Lazy initialize expensive resources
composable-lazy-init - - Use provide/inject for deep prop drilling
composable-provide-inject
- - 每个composable只处理一个关注点
composable-single-responsibility - - 返回refs,而非响应式对象
composable-return-refs - - 在composable中处理清理逻辑
composable-cleanup - - 延迟初始化开销大的资源
composable-lazy-init - - 使用provide/inject解决深层props透传问题
composable-provide-inject
6. State Management (MEDIUM)
6. 状态管理(中)
- - Split stores by domain
state-pinia-stores - - Use getters for computed state
state-getters - - Keep mutations simple, logic in actions
state-actions-mutations - - Cleanup store subscriptions
state-subscription-cleanup
- - 按领域拆分stores
state-pinia-stores - - 对计算后的状态使用getters
state-getters - - 保持mutations简单,逻辑放在actions中
state-actions-mutations - - 清理store订阅
state-subscription-cleanup
7. Async & Data Fetching (LOW-MEDIUM)
7. 异步与数据获取(中低)
- - Use Suspense for async component loading
async-suspense - - Handle async errors gracefully
async-error-boundaries - - Implement SWR pattern for data fetching
async-stale-while-revalidate - - Cancel pending requests on unmount
async-abort-controller
- - 使用Suspense处理异步组件加载
async-suspense - - 优雅处理异步错误
async-error-boundaries - - 实现SWR模式进行数据获取
async-stale-while-revalidate - - 在组件卸载时取消待处理请求
async-abort-controller
8. Advanced Patterns (LOW)
8. 高级模式(低)
- - Create directives for DOM manipulation
advanced-custom-directives - - Use render functions for dynamic templates
advanced-render-functions - - Use Teleport for modals and overlays
advanced-teleport - - Optimize list transitions
advanced-transition-groups
- - 创建自定义指令处理DOM操作
advanced-custom-directives - - 使用渲染函数处理动态模板
advanced-render-functions - - 使用Teleport处理模态框与浮层
advanced-teleport - - 优化列表过渡效果
advanced-transition-groups
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/reactivity-ref-vs-reactive.md
rules/component-v-memo.md
rules/_sections.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单个规则文件获取详细说明与代码示例:
rules/reactivity-ref-vs-reactive.md
rules/component-v-memo.md
rules/_sections.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景信息与参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有展开规则的完整指南:
AGENTS.md