jpskill.com
🛠️ 開発・MCP コミュニティ

web-framework-vue-composition-api

Vue 3のComposition APIを活用し、リアクティビティやコンポーザブル、ライフサイクルといった要素を組み合わせて、効率的で再利用可能なVue.jsのWebアプリケーションを構築するSkill。

📜 元の英語説明(参考)

Vue 3 Composition API patterns, reactivity primitives, composables, lifecycle hooks

🇯🇵 日本人クリエイター向け解説

一言でいうと

Vue 3のComposition APIを活用し、リアクティビティやコンポーザブル、ライフサイクルといった要素を組み合わせて、効率的で再利用可能なVue.jsのWebアプリケーションを構築するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o web-framework-vue-composition-api.zip https://jpskill.com/download/10286.zip && unzip -o web-framework-vue-composition-api.zip && rm web-framework-vue-composition-api.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10286.zip -OutFile "$d\web-framework-vue-composition-api.zip"; Expand-Archive "$d\web-framework-vue-composition-api.zip" -DestinationPath $d -Force; ri "$d\web-framework-vue-composition-api.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して web-framework-vue-composition-api.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → web-framework-vue-composition-api フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Vue 3 Composition API

クイックガイド: 全てのコンポーネントで <script setup> を使用してください。プリミティブには ref()、オブジェクトには reactive() を使用してください。再利用可能なロジックはコンポーザブル (use* 関数) に抽出してください。副作用のクリーンアップは onUnmounted で行ってください。v-model には defineModel() (3.4 以降)、DOM 参照には useTemplateRef() (3.5 以降)、古い非同期処理のキャンセルには onWatcherCleanup() (3.5 以降) を使用してください。分割代入された props は watch() でゲッターラッパーを必要とします。


<critical_requirements>

重要: この Skill を使用する前に

全てのコードは CLAUDE.md のプロジェクト規約に従う必要があります (kebab-case, 名前付きエクスポート, インポート順, import type, 名前付き定数)

(全ての新しい Vue コンポーネントで <script setup> 構文を必ず使用してください)

(全ての副作用 (タイマー, リスナー, サブスクリプション) を onUnmounted で必ずクリーンアップしてください)

(プリミティブには ref()、オブジェクトには reactive() を必ず使用してください - ref の値には .value でアクセスしてください)

(全てのコンポーザブル関数には Vue の規約に従い use をプレフィックスとして必ず付けてください)

(分割代入された props は watch() でゲッターでラップする必要があります - watch(count, ...) ではなく watch(() => count, ...) としてください)

</critical_requirements>


自動検出: Vue 3 Composition API, script setup, ref, reactive, computed, watch, watchEffect, composables, onMounted, onUnmounted, defineProps, defineEmits, defineExpose, defineModel, useTemplateRef, useId, onWatcherCleanup, provide, inject, Suspense

使用場面:

  • Composition API を使用した Vue 3 コンポーネントの構築
  • 再利用可能なコンポーザブル (use* 関数) の作成
  • ref/reactive によるリアクティブな状態の管理
  • コンポーネントのライフサイクルと副作用の処理
  • Vue コンポーネントとの TypeScript 統合

カバーする主要なパターン:

  • Script setup 構文とコンパイラマクロ (defineProps, defineEmits, defineExpose)
  • リアクティビティプリミティブ (ref, reactive, computed, watch, watchEffect)
  • ロジック再利用のためのコンポーザブルパターン
  • v-model バインディングのための defineModel() (Vue 3.4 以降)
  • useTemplateRef(), useId(), onWatcherCleanup() (Vue 3.5 以降)
  • ゲッター要件のあるリアクティブな props の分割代入 (Vue 3.5 以降)
  • 依存性注入のための Provide/Inject
  • 非同期コンポーネントと Suspense

使用しない場面:

  • ロジックの抽出から恩恵を受けないコンポーネント
  • チームに Composition API の経験がない場合 (段階的な導入を検討してください)

<philosophy>

哲学

Composition API は、オプションタイプ (data, methods, computed) ではなく、論理的な関心事によってコードを整理することを可能にします。これにより、複雑なコンポーネントの保守性が向上し、コンポーザブルを通じて強力なロジックの再利用が可能になります。

コア原則:

  1. 設定よりも構成 - オプション全体に分割するのではなく、関連するロジックをグループ化します
  2. 明示的なリアクティビティ - 状態は ref() および reactive() を介して明示的にリアクティブになります
  3. コンポーザブルによるロジックの再利用 - コンポーネント間でステートフルなロジックを抽出して共有します
  4. TypeScript ファースト - 過剰なアノテーションなしで型が自然に流れます

</philosophy>


<patterns>

コアパターン

パターン 1: Props と Emits を使用した Script Setup

<script setup> 内の全ての変数/関数は、テンプレートで自動的に利用可能になります。型安全なインターフェースのために、definePropsdefineEmits で TypeScript ジェネリクスを使用してください。

<script setup lang="ts">
import { ref, computed } from "vue";

const props = defineProps<{
  userId: string;
  initialCount?: number;
}>();

const emit = defineEmits<{
  update: [value: number];
  submit: [];
}>();

const count = ref(props.initialCount ?? 0);
const doubleCount = computed(() => count.value * 2);

function increment() {
  count.value++;
  emit("update", count.value);
}
</script>

良い点: 明示的な return は不要、TypeScript の型が自然に流れる、名前付きタプル emit 構文 (Vue 3.3 以降) はペイロードを自己文書化します

ロード/エラー処理を含む完全なコンポーネントについては、examples/core.md を参照してください。


パターン 2: リアクティビティ - ref vs reactive

プリミティブと再割り当て可能な値には ref()、ネストされたプロパティを持つオブジェクトには reactive() を使用してください。スクリプトでは .value を介して ref の値にアクセスします。テンプレートは自動的にアンラップします。

const count = ref(0); // プリミティブ -> ref
count.value++; // スクリプトでは .value

const state = reactive({
  // ネストされたオブジェクト -> reactive
  user: null as User | null,
  settings: { theme: "light" },
});
state.settings.theme = "dark"; // 直接アクセス、.value は不要

注意点: reactive() を分割代入するとリアクティビティが失われます - 分割代入する必要がある場合は toRefs(state) を使用してください。

ref/reactive/computed のパターンとアンチパターンについては、examples/reactivity.md を参照してください。


パターン 3: Watch と WatchEffect

Nuxt を使用している場合はスキップしてください — 代わりに useFetch または useAsyncData を使用してください。

古い値へのアクセスを提供する明示的なソースには watch() を使用してください。すぐに実行される自動依存関係追跡には watchEffect() を使用してください。古い非同期処理をキャンセルするには onWatcherCleanup() (Vue 3.5 以降) を使用してください。

// watch: 明示的なソース、古い値へのアクセス
watch(searchQuery, async (newQuery, oldQuery) => {
  /* ... */
});

// watchEffect: 依存関係を自動追跡、すぐに実行
watchEffect(async () => {
  if (userId.value) userData.value = await fetchUser(userId.value);
});

// クリーンアップ: 古いリクエストをキャンセル (Vue 3.5 以降)
watch(searchQuery, async (query) => {
  const controller = new AbortController();
  onWatcherCleanup(() => controller.abort());
  const res = await fetch(`/api/search?q=${query}`, {
    signal: controller.signal,
  });
});

注意点: リアクティブオブジェクトのプロパティを監視するには、ゲッターを使用してください: watch(state.count, ...) ではなく watch(() => state.count, ...) としてください。

完全な onWatcherCleanup パターンについては、examples/vue-3-5-features.md を参照してください。


パターン 4: ライフサイクルとクリーンアップ

常に onMounted のセットアップと onUnmounted のクリーンアップをペアにしてください。タイマー、リスナー、オブザーバー、WebSocket - 開いたものは全て閉じる必要があります。

const POLL_INTERVAL_MS = 5000;
let intervalId: ReturnType<typeof setInterval> | null = null;

onMounted(() => {
  intervalId = setInterval(fetchData, POLL_INTERVAL_MS);
});

onUn
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Vue 3 Composition API

Quick Guide: Use <script setup> for all components. ref() for primitives, reactive() for objects. Extract reusable logic into composables (use* functions). Clean up side effects in onUnmounted. Use defineModel() for v-model (3.4+), useTemplateRef() for DOM refs (3.5+), onWatcherCleanup() to cancel stale async work (3.5+). Destructured props require getter wrappers in watch().


<critical_requirements>

CRITICAL: Before Using This Skill

All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering, import type, named constants)

(You MUST use <script setup> syntax for all new Vue components)

(You MUST clean up all side effects (timers, listeners, subscriptions) in onUnmounted)

(You MUST use ref() for primitives and reactive() for objects - access ref values via .value)

(You MUST prefix all composable functions with use following Vue conventions)

(You MUST wrap destructured props in a getter for watch() - watch(() => count, ...) not watch(count, ...))

</critical_requirements>


Auto-detection: Vue 3 Composition API, script setup, ref, reactive, computed, watch, watchEffect, composables, onMounted, onUnmounted, defineProps, defineEmits, defineExpose, defineModel, useTemplateRef, useId, onWatcherCleanup, provide, inject, Suspense

When to use:

  • Building Vue 3 components using Composition API
  • Creating reusable composables (use* functions)
  • Managing reactive state with ref/reactive
  • Handling component lifecycle and side effects
  • TypeScript integration with Vue components

Key patterns covered:

  • Script setup syntax and compiler macros (defineProps, defineEmits, defineExpose)
  • Reactivity primitives (ref, reactive, computed, watch, watchEffect)
  • Composables pattern for logic reuse
  • defineModel() for v-model binding (Vue 3.4+)
  • useTemplateRef(), useId(), onWatcherCleanup() (Vue 3.5+)
  • Reactive props destructure with getter requirement (Vue 3.5+)
  • Provide/Inject for dependency injection
  • Async components and Suspense

When NOT to use:

  • Components that don't benefit from logic extraction
  • When team has no Composition API experience (consider gradual adoption)

<philosophy>

Philosophy

The Composition API enables organizing code by logical concern rather than by option type (data, methods, computed). This makes complex components more maintainable and enables powerful logic reuse through composables.

Core principles:

  1. Composition over configuration - Group related logic together instead of splitting across options
  2. Explicit reactivity - State is explicitly reactive via ref() and reactive()
  3. Logic reuse via composables - Extract and share stateful logic between components
  4. TypeScript-first - Types flow naturally without excessive annotations

</philosophy>


<patterns>

Core Patterns

Pattern 1: Script Setup with Props and Emits

All variables/functions in <script setup> are automatically available in the template. Use TypeScript generics with defineProps and defineEmits for type-safe interfaces.

<script setup lang="ts">
import { ref, computed } from "vue";

const props = defineProps<{
  userId: string;
  initialCount?: number;
}>();

const emit = defineEmits<{
  update: [value: number];
  submit: [];
}>();

const count = ref(props.initialCount ?? 0);
const doubleCount = computed(() => count.value * 2);

function increment() {
  count.value++;
  emit("update", count.value);
}
</script>

Why good: No explicit return needed, TypeScript types flow naturally, named tuple emit syntax (Vue 3.3+) self-documents payloads

See examples/core.md for a complete component with loading/error handling.


Pattern 2: Reactivity - ref vs reactive

ref() for primitives and reassignable values, reactive() for objects with nested properties. Access ref values via .value in script; templates unwrap automatically.

const count = ref(0); // Primitive -> ref
count.value++; // .value in script

const state = reactive({
  // Nested object -> reactive
  user: null as User | null,
  settings: { theme: "light" },
});
state.settings.theme = "dark"; // Direct access, no .value

Gotcha: Destructuring reactive() loses reactivity - use toRefs(state) if you need to destructure.

See examples/reactivity.md for ref/reactive/computed patterns and anti-patterns.


Pattern 3: Watch and WatchEffect

Skip if using Nuxt — use useFetch or useAsyncData instead.

watch() for explicit sources with access to old values. watchEffect() for automatic dependency tracking that runs immediately. Use onWatcherCleanup() (Vue 3.5+) to cancel stale async work.

// watch: explicit source, access to old value
watch(searchQuery, async (newQuery, oldQuery) => {
  /* ... */
});

// watchEffect: auto-tracks dependencies, runs immediately
watchEffect(async () => {
  if (userId.value) userData.value = await fetchUser(userId.value);
});

// Cleanup: cancel stale requests (Vue 3.5+)
watch(searchQuery, async (query) => {
  const controller = new AbortController();
  onWatcherCleanup(() => controller.abort());
  const res = await fetch(`/api/search?q=${query}`, {
    signal: controller.signal,
  });
});

Gotcha: Watch reactive object properties with a getter: watch(() => state.count, ...) not watch(state.count, ...).

See examples/vue-3-5-features.md for complete onWatcherCleanup patterns.


Pattern 4: Lifecycle and Cleanup

Always pair onMounted setup with onUnmounted cleanup. Timers, listeners, observers, WebSockets - anything opened must be closed.

const POLL_INTERVAL_MS = 5000;
let intervalId: ReturnType<typeof setInterval> | null = null;

onMounted(() => {
  intervalId = setInterval(fetchData, POLL_INTERVAL_MS);
});

onUnmounted(() => {
  if (intervalId) {
    clearInterval(intervalId);
    intervalId = null;
  }
});

See examples/lifecycle.md for WebSocket reconnection and event listener cleanup patterns.


Pattern 5: Composables

Extract reusable stateful logic into use* functions. Return objects with refs (not bare values) so destructuring preserves reactivity.

export function useCounter(options: UseCounterOptions = {}) {
  const { initialValue = 0, min = -Infinity, max = Infinity } = options;
  const count = ref(initialValue);
  const isAtMax = computed(() => count.value >= max);

  function increment() {
    if (count.value < max) count.value++;
  }
  function reset() {
    count.value = initialValue;
  }

  return { count, isAtMax, increment, reset }; // Return object with refs
}

Async composables should accept MaybeRefOrGetter<T> inputs (use toValue() to normalize) and return { data, error, isLoading } refs.

See examples/composables.md for useFetch, useLocalStorage, useDebounce, and useIntersectionObserver implementations.


Pattern 6: defineModel for v-model (Vue 3.4+)

Replaces the defineProps + defineEmits boilerplate for two-way binding. Returns a ref-like value that syncs with the parent.

<script setup lang="ts">
const model = defineModel<string>(); // Single v-model
const firstName = defineModel<string>("firstName"); // Named v-model
const [model, modifiers] = defineModel<string>({
  // With modifiers
  set(value) {
    return modifiers.capitalize
      ? value.charAt(0).toUpperCase() + value.slice(1)
      : value;
  },
});
</script>

See examples/vue-3-5-features.md for complete defineModel examples with named models and modifiers.


Pattern 7: Template Refs (Vue 3.5+)

useTemplateRef() separates template refs from reactive refs. Use for dynamic ref names and in composables. Traditional ref() still works for simple static refs.

<script setup lang="ts">
const inputRef = useTemplateRef<HTMLInputElement>("myInput");
onMounted(() => inputRef.value?.focus());
</script>
<template>
  <input ref="myInput" type="text" />
</template>

For child component refs: Use defineExpose() to declare the public API, then ref<InstanceType<typeof Child>>() in the parent.

See examples/define-expose.md for form validation with exposed methods and examples/vue-3-5-features.md for useTemplateRef in composables.


Pattern 8: useId for Accessible IDs (Vue 3.5+)

Generates SSR-safe unique IDs for form labels and ARIA attributes. Each call produces a different ID. Must be called in setup (not in computed).

<script setup lang="ts">
const id = useId();
</script>
<template>
  <label :for="id">Email</label>
  <input :id="id" type="email" />
</template>

See examples/vue-3-5-features.md for multi-field forms and ARIA patterns.


Pattern 9: Reactive Props Destructure (Vue 3.5+)

Destructured props are automatically reactive. Use JavaScript default syntax instead of withDefaults(). The critical gotcha: destructured props require a getter wrapper in watch().

<script setup lang="ts">
const {
  title,
  count = 0,
  items = () => [],
} = defineProps<{
  title: string;
  count?: number;
  items?: string[];
}>();

// CORRECT: getter wrapper
watch(
  () => count,
  (newCount) => {
    /* ... */
  },
);

// WRONG: passes value, not reactive source
// watch(count, ...) // Never triggers!
</script>

See examples/vue-3-5-features.md for complete reactive destructure examples.


Pattern 10: Provide/Inject

Type-safe dependency injection to avoid prop drilling. Define InjectionKey<T> symbols in a separate file, provide in ancestor, inject in descendant with an explicit error for missing providers.

// injection-keys.ts
export const THEME_KEY: InjectionKey<ThemeContext> = Symbol("theme");

// Provider: provide(THEME_KEY, { theme, toggleTheme });
// Consumer: const ctx = inject(THEME_KEY);
//           if (!ctx) throw new Error("Must be used within ThemeProvider");

See examples/provide-inject.md for a complete theme provider/consumer pattern.


Pattern 11: Async Components and Suspense

defineAsyncComponent for code-splitting. Top-level await in <script setup> makes a component async (requires <Suspense> in parent). Use onErrorCaptured at the Suspense boundary for error handling.

const LOADING_DELAY_MS = 200;
const LOAD_TIMEOUT_MS = 10000;

const HeavyChart = defineAsyncComponent({
  loader: () => import("@/components/HeavyChart.vue"),
  loadingComponent: LoadingSpinner,
  delay: LOADING_DELAY_MS,
  timeout: LOAD_TIMEOUT_MS,
});

See examples/async.md for Suspense boundaries with error handling.

</patterns>


Detailed Resources:


<red_flags>

RED FLAGS

High Priority Issues:

  • Missing cleanup in onUnmounted - timers, listeners, subscriptions, WebSockets cause memory leaks
  • Accessing ref.value in template - templates auto-unwrap refs, writing .value in templates is wrong
  • Destructuring reactive() without toRefs() - loses reactivity silently
  • Watching destructured prop directly - watch(count, ...) never triggers, use watch(() => count, ...)

Medium Priority Issues:

  • Watch without async cleanup - causes race conditions; use onWatcherCleanup() (3.5+) or the cleanup callback
  • Using provide() with string keys instead of typed InjectionKey<T> symbols - loses type safety
  • Returning bare values from composables instead of an object with refs - breaks destructuring reactivity

Gotchas & Edge Cases:

  • Refs in reactive objects are auto-unwrapped at root level, but NOT in arrays or Map/Set
  • watchEffect runs immediately; watch is lazy by default
  • Computed values are read-only by default; use getter/setter object for writable computed
  • Top-level await makes a component async and requires <Suspense> in parent
  • Provide values are not reactive by default - wrap in ref() or reactive() if consumers need reactivity
  • onUnmounted won't run if component errors during setup - use error boundaries for critical cleanup
  • useId() must not be called in computed - it generates a new ID each call
  • defineModel returns a ref - use .value in script, auto-unwrapped in template

</red_flags>


<critical_reminders>

CRITICAL REMINDERS

All code must follow project conventions in CLAUDE.md

(You MUST use <script setup> syntax for all new Vue components)

(You MUST clean up all side effects (timers, listeners, subscriptions) in onUnmounted)

(You MUST use ref() for primitives and reactive() for objects - access ref values via .value)

(You MUST prefix all composable functions with use following Vue conventions)

(You MUST wrap destructured props in a getter for watch() - watch(() => count, ...) not watch(count, ...))

Failure to follow these rules will cause memory leaks, broken reactivity, and unmaintainable component APIs.

</critical_reminders>