vue-composition-api-best-practices

Vue 3 组合式 API 与 <script setup> 最佳实践。涵盖代码组织、useXxx 模式、组合式函数设计、Store 集成、响应性优化及功能提取。

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "vue-composition-api-best-practices" with this command: npx skills add github.com/acanzaima/vue-composition-api-best-practices

Vue 3 组合式 API 与 <script setup>、TypeScript 集成及代码组织模式的最佳实践。

版本兼容性

本技能面向 Vue 3.3+,部分功能需要更高的次版本号:

特性最低版本参考
defineOptions3.3+script-setup
toValue()3.3+script-setup
defineModel3.4+script-setup
watch 搭配 once 选项3.4+reactivity
useTemplateRef()3.5+script-setup
useId()3.5+script-setup
onWatcherCleanup()3.5+reactivity

快速决策表

问题参考阅读
我的 <script setup> 很乱,难以浏览SFC 代码组织
某个功能的逻辑分散在很多行中useXxx 函数模式
相同逻辑在多个组件中重复功能提取
两个功能相互影响,但不知道正确的处理模式跨功能依赖
Props/emits 类型安全、defineModel 使用script setup 最佳实践
在 hooks/工具函数/插件中无法访问 Store组件外 Store 访问
如何组织新的组合式函数文件组合式函数设计模式
页面感觉慢,可能是响应性问题响应性与性能
如何测试组合式函数组合式函数测试
类型安全的 provide/injectscript setup - Provide/Inject

代码组织

TypeScript 与 Script Setup

Store 集成

组合式函数设计

响应性与性能

测试


速查表

SFC 代码组织顺序(11 步)

1. defineOptions     → 组件名称
2. defineProps       → Props 类型声明
3. defineModel       → 双向绑定(3.4+)
4. inject            → 注入依赖
5. defineEmits       → 事件类型声明
6. Store 声明         → useXxxStore()
7. 外部 hooks         → useI18n()、useDesign() 等
8. 功能声明           → const { ... } = useFeature()
9. provide           → 提供依赖
10. defineExpose     → 暴露公共 API
11. 功能实现          → function useFeature() {}

响应式 API 选择

基本类型       → ref
需要深层响应    → ref
大型对象/动态组件 → shallowRef
不需要重新赋值   → reactive(谨慎使用)
永不响应式      → markRaw + shallowRef

Store 访问规则

组件内 (<script setup>)  → useAppStore()
组件外 (hooks/utils/plugins) → useAppStoreWithOut()
解构保持响应式            → storeToRefs(store)

反模式 TOP 5

#反模式正确做法
1解构 props → 丢失响应式toRefs(props) 或直接用 props.xxx
2组件外用 useXxxStore()useXxxStoreWithOut()
3ref 用于动态组件/大对象shallowRef
4混用 Options API + script setup只选一种风格
5事件监听不清理onUnmounted 中移除 / 使用 VueUse 的 useEventListener

依赖模式速查

场景推荐模式
父子组件通信Props + Emits
兄弟功能交互回调参数(onXxxChange
跨层级多对多事件总线(useEmitt
共享状态Store 桥接组合式函数
功能编排组合式函数编排模式

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

Rau — 前端全栈AI时代工程心智

Rau — 顶级前端工程师虚拟心智模型。 融合 Guillermo Rauch(Vercel CEO / Next.js 创始人)× Addy Osmani(Google Chrome DevEx 负责人) 的工程智慧:渐进披露复杂度 × Make it work/right/fast × 系统心态 × AI时代...

Registry SourceRecently Updated
1450Profile unavailable
Coding

TypeScript Config Generator

生成专业的 TypeScript 配置,支持严格模式、React、Node.js、Webpack 等多种场景,一键生成最佳实践配置。

Registry SourceRecently Updated
6120Profile unavailable
Coding

AI Intelligence Hub - Real-time Model Capability Tracking

Real-time AI model capability tracking via leaderboards (LMSYS Arena, HuggingFace, etc.) for intelligent compute routing and cost optimization

Registry SourceRecently Updated
4670Profile unavailable
Coding

Performance Engineering System

Complete performance engineering system — profiling, optimization, load testing, capacity planning, and performance culture. Use when diagnosing slow applica...

Registry SourceRecently Updated
5950Profile unavailable