composables/ directory are automatically imported throughout your application.
What Are Composables?
Composables are functions that:- Follow the
useXxxnaming convention - Can use Vue’s reactivity system (ref, reactive, computed)
- Encapsulate reusable logic
- Are automatically imported in Nuxt 3
Auto-import: All files in
composables/ are automatically available without explicit imports. Just call useApi() or useServiceOrders() anywhere in your app.useApi Composable
TheuseApi composable provides a configured Axios instance for making HTTP requests to the backend API.
Full Source Code
composables/useAPI.ts
Key Features
Runtime Configuration
Runtime Configuration
Reads the API base URL from Nuxt’s runtime config:The base URL is defined in
nuxt.config.ts:Client-Side Token Handling
Client-Side Token Handling
Retrieves the authentication token from localStorage (only on client):Why check
import.meta.client?- Prevents errors during server-side rendering
- localStorage is only available in the browser
Authentication Headers
Authentication Headers
Automatically includes the Bearer token in all requests:If a token exists, adds:
Authorization: Bearer <token>Usage Examples
useServiceOrders Composable
TheuseServiceOrders composable manages service order state and provides helper functions for working with orders.
Full Source Code
composables/useServiceOrders.ts
Key Features
Reactive State
Reactive State
Uses Vue’s
ref() to create reactive state:serviceOrders: Array of all service ordersselectedOrder: Currently selected order for viewing/editing
Helper Functions
Helper Functions
openModal(isNew, orderObject)
- Prepares the selected order for modal display
- If
isNewis true, creates an empty order - Otherwise, uses the provided order object
- Returns a new empty ServiceOrder object
- Used when creating new orders
- Ensures all required fields have default values
TypeScript Integration
TypeScript Integration
Uses the Provides type safety and autocomplete for order data.
ServiceOrder type from types/ServiceOrder.ts:Usage in Pages
pages/serviceorders/index.vue
- Destructures reactive state and functions from the composable
- Uses
selectedOrderto track which order is being viewed/edited - Calls
openModal()to prepare orders for display - Fetches real data from API and merges with composable state
Creating Custom Composables
Follow this pattern to create your own composables:composables/useExample.ts
Best Practices
Naming Convention
Always prefix composables with
use (e.g., useApi, useServiceOrders)Single Responsibility
Each composable should focus on one specific area of functionality
Return Object
Return an object with all state and functions you want to expose
Type Safety
Use TypeScript types for better developer experience and fewer bugs
Composables vs Stores
| Feature | Composables | Pinia Stores |
|---|---|---|
| State Sharing | New instance per call | Singleton (shared state) |
| Best For | Component-specific logic | Global application state |
| Reactivity | Yes | Yes |
| Auto-import | Yes (Nuxt) | Yes (with @pinia/nuxt) |
| Example | useApi(), useServiceOrders() | useAuthStore() |
When to use which?
- Use composables for reusable logic that doesn’t need to be shared globally
- Use stores for state that multiple components need to access and modify
Next Steps
API Integration
Learn how to make API calls and handle responses
State Management
Understand global state with Pinia stores