2021-10-11 12:57:54 +00:00
---
2022-10-06 09:15:30 +00:00
navigation.icon: IconDirectory
2021-10-11 12:57:54 +00:00
title: 'composables'
2022-11-21 15:51:39 +00:00
head.title: 'composables/'
2022-10-06 09:15:30 +00:00
description: Use the composables/ directory to auto-import your Vue composables into your application.
2021-10-11 12:57:54 +00:00
---
2022-08-13 07:27:04 +00:00
# Composables Directory
2021-10-11 12:57:54 +00:00
2023-07-07 16:24:09 +00:00
Nuxt 3 uses the [`composables/` directory ](/docs/guide/directory-structure/composables ) to automatically import your Vue composables into your application using [auto-imports ](/docs/guide/concepts/auto-imports )!
2021-10-20 09:47:18 +00:00
2022-09-13 15:35:38 +00:00
Under the hood, Nuxt auto generates the file `.nuxt/imports.d.ts` to declare the types.
2022-04-29 09:36:59 +00:00
2022-11-09 09:43:16 +00:00
Be aware that you have to run `nuxi prepare` , `nuxi dev` or `nuxi build` in order to let Nuxt generate the types. If you create a composable without having the dev server running, TypeScript will throw an error, such as `Cannot find name 'useBar'.`
2022-04-29 09:36:59 +00:00
2022-08-22 15:33:28 +00:00
## Usage
2022-07-21 14:04:50 +00:00
**Method 1:** Using named export
2021-10-20 09:47:18 +00:00
```js [composables/useFoo.ts]
2021-10-22 10:17:09 +00:00
export const useFoo = () => {
2021-10-20 09:47:18 +00:00
return useState('foo', () => 'bar')
}
```
2022-07-21 14:04:50 +00:00
**Method 2:** Using default export
2021-10-20 09:47:18 +00:00
```js [composables/use-foo.ts or composables/useFoo.ts]
2022-01-18 16:36:29 +00:00
// It will be available as useFoo() (camelCase of file name without extension)
2021-10-20 09:47:18 +00:00
export default function () {
2021-12-20 10:36:25 +00:00
return useState('foo', () => 'bar')
2021-10-20 09:47:18 +00:00
}
```
2022-07-21 14:04:50 +00:00
**Usage:** You can now use auto imported composable in `.js` , `.ts` and `.vue` files
2021-10-20 09:47:18 +00:00
```vue [app.vue]
2023-07-18 10:31:45 +00:00
< script setup lang = "ts" >
const foo = useFoo()
< / script >
2021-10-20 09:47:18 +00:00
< template >
< div >
{{ foo }}
< / div >
< / template >
```
2022-04-09 09:25:13 +00:00
2023-06-27 11:27:11 +00:00
::LinkExample{link="/docs/examples/features/auto-imports"}
2022-10-25 09:33:09 +00:00
::
2022-07-21 14:04:50 +00:00
2022-08-22 15:33:28 +00:00
## Examples
### Nested Composables
You can use a composable within another composable using auto imports:
```js [composables/test.ts]
export const useFoo = () => {
const nuxtApp = useNuxtApp()
const bar = useBar()
}
```
### Access plugin injections
2022-11-16 10:04:28 +00:00
You can access [plugin injections ](/docs/guide/directory-structure/plugins#automatically-providing-helpers ) from composables:
2022-08-22 15:33:28 +00:00
```js [composables/test.ts]
export const useHello = () => {
const nuxtApp = useNuxtApp()
return nuxtApp.$hello
}
```
2022-08-13 07:27:04 +00:00
## How Files Are Scanned
2022-07-21 14:04:50 +00:00
2023-07-07 16:24:09 +00:00
Nuxt only scans files at the top level of the [`composables/` directory ](/docs/guide/directory-structure/composables ), e.g.:
2022-07-21 14:04:50 +00:00
```bash
composables
| - index.ts // scanned
2022-08-16 08:14:03 +00:00
| - useFoo.ts // scanned
2022-07-21 14:04:50 +00:00
| - nested
| --- utils.ts // not scanned
```
2022-08-16 08:14:03 +00:00
Only `composables/index.ts` and `composables/useFoo.ts` would be searched for imports.
2022-07-21 14:04:50 +00:00
2022-08-16 08:14:03 +00:00
To get auto imports working for nested modules, you could either re-export them (recommended) or configure the scanner to include nested directories:
2022-07-21 14:04:50 +00:00
2022-08-16 08:14:03 +00:00
**Example:** Re-export the composables you need from the `composables/index.ts` file:
2022-07-21 14:04:50 +00:00
2022-08-16 08:14:03 +00:00
```ts [composables/index.ts]
2022-07-21 14:04:50 +00:00
// Enables auto import for this export
2022-08-16 08:14:03 +00:00
export { utils } from './nested/utils.ts'
2022-07-21 14:04:50 +00:00
```
2022-08-16 08:14:03 +00:00
**Example:** Scan nested directories inside the `composables/` folder:
2022-07-21 14:04:50 +00:00
```ts [nuxt.config.ts]
2022-08-16 08:14:03 +00:00
export default defineNuxtConfig({
2022-08-23 14:22:11 +00:00
imports: {
2022-07-21 14:04:50 +00:00
dirs: [
2022-08-16 08:14:03 +00:00
// Scan top-level modules
'composables',
// ... or scan modules nested one level deep with a specific name and file extension
'composables/*/index.{ts,js,mjs,mts}',
// ... or scan all modules within given directory
'composables/**'
2022-07-21 14:04:50 +00:00
]
}
})
```