mcpField
V
vue-best-practices

vue-best-practices

github.com/ejirocodes開発6284 回インストール18 週間前に更新
リポジトリ

vue-best-practices について

vue-best-practicesは、Vue 3開発者向けのリファレンススキルです。TypeScript、コンポーネント、ツール、テストの実践方法に加え、Vue 3.5のパターンやよくある誤りを扱います。

主な機能

  1. 01TypeScriptとコンポーネントの型
  2. 02VolarとIDEの設定
  3. 03Vueコンポーネントのパターン
  4. 04ツールとHMRのガイダンス
  5. 05Piniaとルーターのテスト
  6. 06よくある誤りの防止

活用シーン

  1. 01リアクティブなpropsのパターン選択
  2. 02Vueコンポーネントのprops型定義
  3. 03Volarのテンプレート検査設定
  4. 04Piniaストアテストの準備

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/ejirocodes/agent-skills --skill vue-best-practices

インストール後、Claude Code で/vue-best-practicesを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

references/
components.md
testing.md
tooling.md
typescript.md
volar.md

詳細

Vue 3 のベストプラクティス

クイックリファレンス

トピック使用する場面リファレンス
TypeScriptProps の抽出、ジェネリックコンポーネント、useTemplateRef、JSDoc、リアクティブな props の分割代入typescript.md
VolarIDE の設定、strictTemplates、CSS モジュール、ディレクティブコメント、Volar 3.0 への移行volar.md
コンポーネントdefineModel、ディープウォッチ、onWatcherCleanup、useId、遅延テレポートcomponents.md
ツールmoduleResolution、HMR SSR、プラグインの重複検出tooling.md
テストPinia ストアのモック、セットアップストア、Vue Router の型付きパラメーターtesting.md

基本的なパターン

コンポーネントの Props を抽出する
import type { ComponentProps } from 'vue-component-type-helpers'
import MyButton from './MyButton.vue'

type Props = ComponentProps<typeof MyButton>
リアクティブな Props の分割代入(Vue 3.5+)
<script setup lang="ts">
// Destructured props are reactive - preferred in Vue 3.5+
const { name, count = 0 } = defineProps<{ name: string; count?: number }>()
</script>
useTemplateRef(Vue 3.5+)
<script setup lang="ts">
import { useTemplateRef, onMounted } from 'vue'

const inputRef = useTemplateRef('input')  // Auto-typed
onMounted(() => inputRef.value?.focus())
</script>
<template><input ref="input" /></template>
onWatcherCleanup(Vue 3.5+)
import { watch, onWatcherCleanup } from 'vue'

watch(query, async (q) => {
  const controller = new AbortController()
  onWatcherCleanup(() => controller.abort())
  await fetch(`/api?q=${q}`, { signal: controller.signal })
})
必須項目を指定した defineModel
// Returns Ref<Item> instead of Ref<Item | undefined>
const model = defineModel<Item>({ required: true })
数値の深さを指定したディープウォッチ
// Vue 3.5+ - watch array mutations without full traversal
watch(items, handler, { deep: 1 })
Pinia ストアのテスト設定
import { createTestingPinia } from '@pinia/testing'
import { vi } from 'vitest'

mount(Component, {
  global: {
    plugins: [createTestingPinia({ createSpy: vi.fn })]
  }
})

よくある間違い

  1. InstanceType<typeof Component>['$props'] を使う - 代わりに ComponentProps を使います
  2. createTestingPinia で createSpy を指定し忘れる - @pinia/testing 1.0+ では必須です
  3. ユニオン型で withDefaults を使う - 代わりにリアクティブな Props の分割代入を使います
  4. 誤った tsconfig で strictTemplates を指定する - ルートではなく tsconfig.app.json に追加します
  5. Volar 3.0 で ts_ls を使う - 代わりに vtsls を使います(Neovim)
  6. 大規模な構造で deep: true を使う - パフォーマンス向上のため、数値で深さを指定します
  7. 分割代入した props を直接監視する - ゲッターでラップします:watch(() => count, ...)
  8. SSR でランダムな ID を使う - ハイドレーションに安全な ID を生成するには useId() を使います

コメント