0Pricing
Vue Academy · Lesson

Importing and Using Composables in script setup

Importing composables, using third-party VueUse composables, organizing imports.

Importing and Using Composables in script setup is a free Vue Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Composables and script setup

Composables are functions that package reactive logic for reuse. With <script setup> using them is as simple as importing and calling at the top level.

Import at the Top Level

Always call a composable at the top level of <script setup> — not inside a condition, loop, or nested function. This ensures Vue registers its reactivity and lifecycle hooks correctly.

<script setup>
import { useCounter } from './useCounter'
const { count, increment } = useCounter()
</script>

Why Top-Level Matters

Composables often register lifecycle hooks like onMounted or set up watchers. These must run during component setup, which only happens if you call the composable synchronously at the top level.

Destructure What You Need

A composable returns an object of reactive values and functions. Destructure only what the component uses; rename to avoid collisions when calling the same composable twice.

<script setup>
import { useMouse } from './useMouse'
const { x: mouseX, y: mouseY } = useMouse()
</script>

Meet VueUse

VueUse is a popular library of ready-made composables. Instead of writing common logic yourself, import a tested composable. Install it once and import what you need.

<script setup>
import { useLocalStorage } from '@vueuse/core'
const name = useLocalStorage('name', 'Alice')
</script>

useLocalStorage in Action

useLocalStorage returns a ref synced to localStorage. Change the ref and the browser storage updates; reload the page and the value persists.

<template>
  <input v-model="name" />
  <p>Saved: {{ name }}</p>
</template>

useMouse for Reactive Coordinates

useMouse tracks the pointer position as reactive refs. It adds and removes the event listener for you, so you never manage cleanup manually.

<script setup>
import { useMouse } from '@vueuse/core'
const { x, y } = useMouse()
</script>

<template>
  <p>{{ x }}, {{ y }}</p>
</template>

Composables Compose

A composable can call other composables. This layering is the heart of the Composition API — build small pieces and combine them into larger features.

import { useMouse } from '@vueuse/core'

export function useMouseDistance() {
  const { x, y } = useMouse()
  const distance = computed(() => Math.hypot(x.value, y.value))
  return { distance }
}

Top-Level await

<script setup> supports await directly at the top level. The component becomes an async setup and the framework can suspend rendering until the promise resolves.

<script setup>
const res = await fetch('/api/user')
const user = await res.json()
</script>

Suspense with Async Setup

A component using top-level await must be wrapped in a <Suspense> boundary by its parent. Suspense shows fallback content until the async setup finishes.

<template>
  <Suspense>
    <AsyncUser />
    <template #fallback>Loading...</template>
  </Suspense>
</template>

Best Practices

  • Name composables with a use prefix.
  • Call them at the top level only.
  • Return refs (or a reactive wrapped in toRefs).
  • Prefer VueUse for common needs before writing your own.

Quick Check

Confirm your understanding of composables.

Recap

You learned to use composables:

  • Import and call them at the top level of <script setup>.
  • Destructure (and rename) what you need.
  • VueUse offers ready-made composables like useLocalStorage and useMouse.
  • Top-level await makes setup async and pairs with <Suspense>.

Frequently asked questions

Is the “Importing and Using Composables in script setup” lesson free?

Yes — the full text of “Importing and Using Composables in script setup” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “Importing and Using Composables in script setup”?

Importing composables, using third-party VueUse composables, organizing imports. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Importing and Using Composables in script setup” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to script setup
  2. defineProps and defineEmits
  3. defineExpose and useAttrs
  4. Importing and Using Composables in script setup
← Back to Vue Academy