vue-patterns
它能做什么
本指南教你如何使用 Vue.js 3、Nuxt 和 Pinia 构建应用。内容包括如何组织代码、创建可复用组件,以及管理应用的状态。
适用场景
- ✓开始一个新的 Vue.js 或 Nuxt 项目时。
- ✓需要更好地组织项目代码时。
- ✓创建可复用组件或管理全局数据时。
技能链接
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/vue-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“在 Vue.js 项目中,文件应该怎样组织?”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会依据提供的指导原则,建议文件夹结构、命名规范,以及 `.vue` 文件中各元素的正确排列顺序。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Vue.js Patterns and Best Practices
Comprehensive guide for Vue.js 3 development using Composition API (<script setup>), covering component design, reactivity, state management, routing, testing, and SSR patterns. Nuxt-specific guidance is included where it differs from vanilla Vue.
When to Activate
Activate this skill when:
- The project uses Vue.js (any version), Nuxt, Vite + Vue, or Pinia.
- The user asks about Vue component architecture, composables, reactivity, or state management.
- Reviewing Vue Single-File Components (
.vuefiles). - Setting up Vue Router, Pinia stores, or Vite/Vitest configuration.
- Discussing Vue-specific performance, security, or SSR patterns.
1. Project Structure
Recommended Layout (Feature-First)
src/
├── api/ # API client and endpoint definitions
├── assets/ # Static assets (images, fonts, icons)
├── components/ # Shared/reusable components
│ ├── base/ # Base UI primitives (Button, Input, Modal)
│ └── features/ # Feature-specific shared components
├── composables/ # Reusable Composition API logic
├── layouts/ # Page layouts (optional)
├── pages/ # Route-level page components
├── router/ # Vue Router configuration
├── stores/ # Pinia stores
├── types/ # TypeScript type definitions
├── utils/ # Pure utility functions
└── App.vue # Root component
File Naming
| Convention | When to Use |
|---|---|
PascalCase.vue |
All components (enforced by vue/multi-word-component-names) |
useCamelCase.ts |
Composables |
camelCase.ts |
Utilities, API clients, types |
kebab-case directories |
Route segments, feature folders |
2. Component Architecture
Single-File Component Order
<script setup lang="ts">
// 1. Imports (vue → ecosystem → absolute → relative)
// 2. Props & Emits & Slots
// 3. Composables
// 4. Local state (ref/reactive)
// 5. Computed properties
// 6. Methods
// 7. Watchers
// 8. Lifecycle hooks
</script>
<template>
<!-- Template content -->
</template>
<style scoped>
/* Scoped styles */
</style>
Presentational vs Container
- Container components: Own data fetching, state, and side effects. Render presentational components.
- Presentational components: Receive props, emit events. No API calls, no store access. Pure rendering.
Props Best Practices
// Type-based props with defaults
interface Props {
label: string;
variant?: "primary" | "secondary";
disabled?: boolean;
items: Item[];
}
const props = withDefaults(defineProps<Props>(), {
variant: "primary",
disabled: false,
});
- Always provide
type, andrequired/defaultwhere appropriate. - Boolean props:
isXxx,hasXxx,canXxx. - Never mutate props — emit events instead.
- For v-model binding, use
defineModel()(Vue 3.4+) ormodelValue+update:modelValue.
Events
const emit = defineEmits<{
submit: [];
"update:modelValue": [value: string];
select: [id: string, index: number];
}>();
- Use kebab-case in templates (
@update:model-value). - Use camelCase in script (
emit("update:modelValue", val)).
3. Composables (Reusable Logic)
Structure
// composables/useDebounce.ts
export function useDebounce<T>(value: MaybeRef<T>, delay: number): Ref<T> {
const debounced = ref(toValue(value)) as Ref<T>;
let timer: ReturnType<typeof setTimeout>;
watch(
() => toValue(value),
(newVal) => {
clearTimeout(timer);
timer = setTimeout(() => { debounced.value = newVal; }, delay);
}
);
onUnmounted(() => clearTimeout(timer));
return readonly(debounced);
}
Rules
- Must start with
useprefix. - Return reactive values (
ref,computed,reactive), never plain primitives. - Accept reactive inputs via
MaybeRef/toRef()/toValue(). - Clean up side effects in
onUnmountedor watcheronCleanup. - No module-scope side effects.
vs Mixins
Composables replace Vue 2 mixins entirely:
- Mixins: Opaque data flow, source-of-truth collisions, name conflicts.
- Composables: Explicit imports, clear return values, composable and tree-shakable.
4. State Management
When to Use What
| Pattern | Use Case |
|---|---|
ref() / reactive() |
Local component state |
| Props + Emits | Parent-child communication |
| Provide / Inject | Theme, config, plugin API |
…
常见问题
什么是 Composition API?
它是在 Vue.js 中组织逻辑代码的一种更灵活的方式,使用 `ref`、`reactive` 和 `watch` 等函数来实现。
展示型组件和容器型组件有什么区别?
容器型组件负责管理数据和逻辑,展示型组件只负责显示信息,并接收指令。
什么时候应该用 Pinia,而不是 `ref` 或 `reactive`?
当数据需要在应用的多个部分之间共享时使用 Pinia;`ref` 和 `reactive` 则用于组件的局部数据。
详情
作者的原始描述
Vue.js 3 Composition API patterns, component architecture, reactivity best practices, Pinia state management, Vue Router navigation, and Nuxt SSR patterns. Activates for Vue, Nuxt, Vite, or Pinia projects. Use when building or reviewing Vue 3, Nuxt, or Pinia code — Composition API, reactivity, or router navigation.
repo: affaan-m/ECC
热度
- GitHub 星标
- 280k
- 排名
- #257
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
accessibility
280k帮你的智能体创建人人都能使用的界面。确保网站和应用对残障人士友好,比如失明或行动不便的用户也能顺畅使用。
agent-architecture-audit
280k让你的智能体检查一切是否正常运行。它能找出并修复 AI 系统中的问题,确保系统按预期工作。
agent-eval
280k帮你的智能体比较不同的 AI 编程工具。它能衡量哪个工具在特定任务上更快、更便宜、更可靠。
agent-harness-construction
280k让你的智能体更好地理解如何完成任务。它会优化智能体使用的工具和指令,提高成功的几率。