mcpField
T
tanstack-vue-router-skilld

tanstack-vue-router-skilld

github.com/skilld-dev開発18499 回インストール3 週間前に更新
リポジトリ

tanstack-vue-router-skilld について

tanstack-vue-router-skilldは、@tanstack/vue-routerでVueアプリをルーティングする開発者向けのリファレンススキルです。型安全なルート、ナビゲーション、データ読み込み、検索パラメーター、SSRを扱います。

主な機能

  1. 01型安全なファイルベース・コードベースのルーティング
  2. 02Vueコンポーザブルとルートコンポーネント
  3. 03ローダー・コンテキスト・リダイレクト
  4. 04検索パラメーターの検証
  5. 05SSRエントリーポイントとコンポーネント

活用シーン

  1. 01型安全なVueナビゲーションの構築
  2. 02認証チェックを伴うルートデータの読み込み
  3. 03検索パラメーターの検証と変換
  4. 04サーバーサイドレンダリングの設定

インストール

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

$npx skills add https://github.com/skilld-dev/vue-ecosystem-skills --skill tanstack-vue-router-skilld

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

同梱ファイル

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

references/
api-surface.md
composables.md
data-loading.md
file-based-routing.md
navigation-links.md
routes.md
search-params.md
ssr.md

詳細

@tanstack/vue-router

Vue 用の型安全なルーターです。準備済みソース:バージョン 1.170.35(package.json:3)。

  • vue >= 3.3.0(peer)および Node >= 20.19 が必要です(package.json:59-88)
  • ランタイム依存関係:@tanstack/[email protected]、@tanstack/[email protected]、@tanstack/vue-store@^0.11.0
  • ESM のみ("type": "module")。main/CJS エントリーポイントはありません
  • エントリーポイント:@tanstack/vue-router、@tanstack/vue-router/ssr/server、@tanstack/vue-router/ssr/client(package.json:29-50)

ドキュメント:https://tanstack.com/router(フレームワークのページは /router/latest/docs/framework/vue/... で確認済み)

重要なルール

  1. ほとんどのコンポーザブルは Ref<T> を返します — スクリプト内では .value を使用し、テンプレートでは自動的にアンラップされます。useRouter()、useNavigate()、useLinkProps()、useAwaited() は ref を返しません。完全な一覧は references/composables.md を参照してください。
  2. 推論されるルート/ルーターの型をキャストしたり、注釈を付けたりしないでください。 型はルートツリーと Register 宣言から取得されます。
  3. 型安全性のためにルーターを登録してください — 登録しないと、Link/useNavigate/useSearch は任意の文字列を受け入れます:
declare module '@tanstack/vue-router' {
  interface Register {
    router: typeof router
  }
}
  1. vue-router ではありません。 vue-router から useRoute/useRouter をインポートしないでください。また、<router-view>/<router-link> も使用しないでください。
  2. beforeLoad/loader は通常の非同期関数です。 Vue のコンポーザブル(ref、computed、ライフサイクルフック)はその中で実行できません。ルーターの context を通じて状態を渡してください。
  3. ルートオプションでは Vue SFC を使用できます。component、errorComponent、notFoundComponent、pendingComponent と、ルーターオプションの default*Component は、.vue ファイルまたは h() 形式の関数コンポーネントを受け取ります(src/route.ts:54-58、src/router.ts:26-65)。

Vite を使ったセットアップ(ファイルベースルーティング)

npm install @tanstack/vue-router
npm install -D @tanstack/router-plugin @vitejs/plugin-vue @vitejs/plugin-vue-jsx
// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { tanstackRouter } from '@tanstack/router-plugin/vite'

export default defineConfig({
  plugins: [
    tanstackRouter({ target: 'vue', autoCodeSplitting: true }), // before vue()
    vue(),
    vueJsx(), // needed for .tsx route files
  ],
})

出典:公式 Vue の例、https://github.com/TanStack/router/blob/main/examples/vue/basic-file-based-sfc/vite.config.ts

// src/main.ts
import { createApp, h } from 'vue'
import { RouterProvider, createRouter } from '@tanstack/vue-router'
import { routeTree } from './routeTree.gen'

const router = createRouter({
  routeTree,
  defaultPreload: 'intent',
  scrollRestoration: true,
})

declare module '@tanstack/vue-router' {
  interface Register {
    router: typeof router
  }
}

createApp({ setup: () => () => h(RouterProvider, { router }) }).mount('#app')

ファイル名、分割ファイルの規則(.route.ts、.component.vue、.lazy.ts)、生成される routeTree.gen.ts については、references/file-based-routing.md を参照してください。

コードベースのルーティング

import { h } from 'vue'
import { createRootRouteWithContext, createRoute, createRouter, Outlet } from '@tanstack/vue-router'

interface RouterContext { auth: { isAuthenticated: boolean } }

const rootRoute = createRootRouteWithContext<RouterContext>()({ component: () => h(Outlet) })

const postsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: 'posts',
  loader: ({ context }) => fetchPosts(context.auth),
})

const routeTree = rootRoute.addChildren([postsRoute])
const router = createRouter({
  routeTree,
  context: { auth: { isAuthenticated: false } },
})

ルートオプション、ルートオブジェクト上のフック、プロパティの順序に関するルールについては、references/routes.md を参照してください。

コンポーザブル

コンポーザブル戻り値備考
useRouter()router instanceref ではありません
useRouterState({ select })Ref<T>再レンダリングを避けるために select を渡します
useNavigate({ from? })functionref ではありません
useSearch({ from })Ref<T>from が一致しない場合はエラーになります
useParams({ from })Ref<T>
useMatch({ from })Ref<T>
useLoaderData({ from })Ref<T>
useLoaderDeps({ from })Ref<T>
useRouteContext({ from })Ref<T>
useLocation()Ref<ParsedLocation>
useMatches() / useParentMatches() / useChildMatches()Ref<Array<Match>>select を受け取ります
useMatchRoute()function呼び出すと Ref<false | params> を返します(src/Matches.tsx:148-172)
useLinkProps(options)link props objectカスタムアンカー向け
useBlocker({ shouldBlockFn })void または Ref<BlockerResolver>リゾルバーが返されるのは withResolver: true の場合のみです
useCanGoBack()Ref<boolean>location.state.__TSR_index !== 0(src/useCanGoBack.ts:4-10)
useAwaited({ promise })[data, promise] tuple遅延データ向け

ルートオブジェクトと getRouteApi('/path') は、同じフックをスコープ済みの状態で公開します:Route.useSearch()、Route.useLoaderData()、Route.useNavigate()、Route.Link(src/route.ts:72-80)。

詳細と select/strict の動作については、references/composables.md を参照してください。

コンポーネント

コンポーネント目的
<RouterProvider :router="router" />ルーターをマウントします。追加の属性でルーターのオプションを更新します(src/RouterProvider.tsx:60-95)
<Link to="..." :params="..." :search="...">型安全なアンカーです。有効状態では data-status="active" と aria-current="page" が設定されます(src/link.tsx:522-525)。デフォルトスロットには { isActive } が渡されます(src/link.tsx:918)
<Outlet />一致した子ルートをレンダリングします(src/Match.tsx:275)
<Navigate to="..." />宣言的なリダイレクトです。onMounted で実行されます(src/useNavigate.tsx:24-40)
<MatchRoute to="..." :fuzzy="true">一致した場合にスロットをレンダリングします。スコープ付きスロットにはパラメーターが渡されます
<Await :promise="p"><Suspense> を使った遅延データです(src/awaited.tsx:26-43)
<Block :should-block-fn="fn">ナビゲーションをブロックします。スコープ付きスロットにはリゾルバーが渡されます(src/useBlocker.tsx:470-491)
<CatchBoundary> / <ErrorComponent>onErrorCaptured によるエラーバウンダリーです
<ClientOnly>マウント後にのみ子要素をレンダリングします
<HeadContent>, <Scripts>, <Html>, <Body>, <Asset>, <ScriptOnce>SSR ドキュメントシェル

リンクのオプション、activeProps(デフォルトは { class: 'active' }、src/link.tsx:477)、プリロード(intent/viewport/render)、linkOptions()、createLink()については、references/navigation-links.mdを参照してください。

データの読み込みとリダイレクト

const route = createFileRoute('/posts/$postId')({
  beforeLoad: async ({ context, params }) => {
    if (!context.auth.isAuthenticated) {
      throw redirect({ to: '/login', search: { from: params.postId } })
    }
  },
  loaderDeps: ({ search: { page } }) => ({ page }),
  loader: ({ deps, params }) => fetchPost(params.postId, deps.page),
  pendingComponent: PendingSpinner, // .vue file works too
})

throw redirect()、isRedirect()、defer() による遅延ストリーミング、コンテキストの型付けについては、references/data-loading.mdを参照してください。

検索パラメーター

import { z } from 'zod' // or plain validators
const route = createFileRoute('/posts')({
  validateSearch: z.object({ page: z.number().default(1) }),
  search: {
    middlewares: [stripSearchParams({ page: 1 })],
  },
})

バリデーター、入力/出力スキーマ、retainSearchParams/stripSearchParams、カスタムシリアライズについては、references/search-params.mdを参照してください。

1.170.x での非推奨項目(代わりに使用)

非推奨置き換え根拠
Route、RootRoute、FileRoute、RouteApi クラスcreateRoute、createRootRoute、createFileRoute、getRouteApisrc/route.ts:95、src/route.ts:220、src/route.ts:460、src/fileRoute.ts:57
rootRouteWithContextcreateRootRouteWithContextsrc/route.ts:414
NotFoundRoute / routerOptions.notFoundRoutenotFoundComponent ルートオプション / defaultNotFoundComponent ルーターオプションhttps://tanstack.com/router/latest/docs/framework/vue/guide/not-found-errors
<ScrollRestoration />createRouter の scrollRestoration: truesrc/ScrollRestoration.tsx:18
beforeLoad/loader の opts.navigatethrow redirect({ to })https://tanstack.com/router/latest/docs/framework/vue/api/router/RouteOptionsType
parseParams/stringifyParamsparams.parse/params.stringify同じ RouteOptionsType ドキュメント
preSearchFilters/postSearchFilterssearch.middlewares同じ RouteOptionsType ドキュメント
FileRouteLoader / 個別の .lazy.ts ローダーファイルローダーをルートファイル内に記述するsrc/fileRoute.ts:153
useBlocker(fn, condition) の旧シグネチャ{ shouldBlockFn } オブジェクトsrc/useBlocker.tsx:139-151

ベストプラクティス

参考資料

コメント