Vue 3 composition api composables

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Vue 3 composition api composables is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. Source: https://vuejs.org/ · https://blog.vuejs.org/posts/vue-3-5

How to use this skill

Skill file

<!--

Source: https://vuejs.org/ · https://blog.vuejs.org/posts/vue-3-5

Authored: June 2026 for the frontend-engineer agent bundle.

-->

Vue 3 — Composition API + Nuxt 3

Vue 3.5+ Composition API with <script setup> is the only style worth writing

in 2026. defineModel simplifies two-way bindings, props destructure preserves

reactivity (3.5+), and Nuxt 3 (Nitro under the hood) brings file-based routes

plus universal rendering.

When to use

"ref", "reactive", "defineModel", "useFetch", "useAsyncData"

Setup

# New Vue 3 SPA (Vite-based)
pnpm create vue@latest
# choose: TypeScript, Vue Router, Pinia, Vitest, Playwright, ESLint

# New Nuxt 3 app
pnpm dlx nuxi@latest init my-app
cd my-app
pnpm install
pnpm dev

Verify: pnpm exec vue --version → 3.5.x. Nuxt: pnpm exec nuxi --version → 3.x.

No API keys required.

Common recipes

Recipe 1 — `<script setup>` boilerplate

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

const count = ref(0);
const doubled = computed(() => count.value * 2);

function increment() {
  count.value++;
}
</script>

<template>
  <button @click="increment">Clicks: {{ count }}</button>
  <p>Doubled: {{ doubled }}</p>
</template>

ref().value is required in JS; templates auto-unwrap.

Recipe 2 — `reactive` for objects, `ref` for primitives

import { ref, reactive } from "vue";

const counter = ref(0);                                // primitive
const user = reactive({ name: "Ada", age: 30 });       // object
const items = ref<string[]>([]);                       // generic

Prefer ref everywhere if unsure — it composes more cleanly with destructuring

(via toRefs).

Recipe 3 — `defineModel` (two-way binding, 3.4+ stable)

<!-- TextInput.vue -->
<script setup lang="ts">
const model = defineModel<string>({ required: true });
</script>

<template>
  <input v-model="model" />
</template>

Parent: <TextInput v-model="email" /> — no manual emit/prop boilerplate.

Recipe 4 — Props destructure with reactivity (3.5+)

<script setup lang="ts">
interface Props {
  name: string;
  age?: number;
}

// In 3.5+ this preserves reactivity (was previously lost on destructure).
const { name, age = 18 } = defineProps<Props>();
</script>

<template>
  <p>{{ name }} ({{ age }})</p>
</template>

Recipe 5 — Composable (`useX`)

// composables/useMouse.ts
import { ref, onMounted, onUnmounted } from "vue";

export function useMouse() {
  const x = ref(0);
  const y = ref(0);
  function update(e: MouseEvent) {
    x.value = e.pageX;
    y.value = e.pageY;
  }
  onMounted(() => window.addEventListener("mousemove", update));
  onUnmounted(() => window.removeEventListener("mousemove", update));
  return { x, y };
}
<script setup lang="ts">
import { useMouse } from "@/composables/useMouse";
const { x, y } = useMouse();
</script>

<template>Mouse is at {{ x }}, {{ y }}</template>

Recipe 6 — `watch` and `watchEffect`

import { ref, watch, watchEffect } from "vue";

const query = ref("");

// explicit watch — fires when `query` changes
watch(query, async (newQ, oldQ) => {
  const res = await fetch(`/api/search?q=${newQ}`);
  // ...
}, { debounce: 300 } as any);

// reactive auto-tracking
watchEffect(async () => {
  if (!query.value) return;
  const res = await fetch(`/api/search?q=${query.value}`);
  // ...
});

Recipe 7 — Suspense + async setup

<!-- AsyncProfile.vue -->
<script setup lang="ts">
const user = await fetch("/api/me").then(r => r.json());
</script>

<template>
  <article><h1>{{ user.name }}</h1></article>
</template>
<!-- parent -->
<template>
  <Suspense>
    <AsyncProfile />
    <template #fallback>Loading...</template>
  </Suspense>
</template>

Recipe 8 — Pinia store (modern Vuex replacement)

// stores/cart.ts
import { defineStore } from "pinia";
import { ref, computed } from "vue";

export const useCart = defineStore("cart", () => {
  const items = ref<{ id: string; qty: number }[]>([]);
  const total = computed(() => items.value.reduce((sum, i) => sum + i.qty, 0));

  function add(id: string) {
    items.value.push({ id, qty: 1 });
  }
  function clear() { items.value = []; }

  return { items, total, add, clear };
});
<script setup lang="ts">
import { useCart } from "@/stores/cart";
const cart = useCart();
</script>

<template>
  <button @click="cart.add('sku-1')">Add ({{ cart.total }})</button>
</template>

Recipe 9 — Nuxt 3 `useFetch` (typed, SSR-safe)

<!-- pages/posts/[slug].vue -->
<script setup lang="ts">
const route = useRoute();
const { data: post, pending, error } = await useFetch(`/api/posts/${route.params.slug}`);
</script>

<template>
  <article v-if="post">
    <h1>{{ post.title }}</h1>
    <div v-html="post.body" />
  </article>
  <p v-else-if="pending">Loading...</p>
  <p v-else-if="error">Failed: {{ error.message }}</p>
</template>

Recipe 10 — Nuxt 3 server route

// server/api/posts/[slug].get.ts
export default defineEventHandler(async (event) => {
  const slug = getRouterParam(event, "slug");
  const post = await usePrisma().post.findUnique({ where: { slug } });
  if (!post) throw createError({ statusCode: 404, statusMessage: "Not found" });
  return post;
});

Recipe 11 — File-based routing (Nuxt)

pages/
  index.vue            # /
  about.vue            # /about
  blog/
    index.vue          # /blog
    [slug].vue         # /blog/[slug]
  [...catch].vue       # 404 catch-all

For SPA without Nuxt, install unplugin-vue-router to get the same convention

on Vite.

Examples

Example 1: Convert Options API to `<script setup>`

Before:

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  props: { id: String },
  data() { return { count: 0 }; },
  computed: { doubled() { return this.count * 2; } },
  methods: { inc() { this.count++; } },
});
</script>

After:

<script setup lang="ts">
import { ref, computed } from "vue";
const { id } = defineProps<{ id: string }>();
const count = ref(0);
const doubled = computed(() => count.value * 2);
const inc = () => count.value++;
</script>

Example 2: Lazy-load a heavy component

<script setup lang="ts">
import { defineAsyncComponent } from "vue";
const HeavyChart = defineAsyncComponent(() => import("@/components/HeavyChart.vue"));
</script>

<template>
  <Suspense>
    <HeavyChart />
    <template #fallback><div class="chart-skeleton" /></template>
  </Suspense>
</template>

Edge cases / gotchas

(Pinia) to preserve it.

inside <script setup>, never imported.

defineModel("query") and v-model:query="...".

useAsyncData is for arbitrary async work. Both dedupe on key.

binding.

composables; calling them in event handlers loses the active instance.

experimentalDefineModel: true. Remove that flag in 3.5+.

(useCart()) inside setup every time.

default mode.

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.