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.
2023-10-18 10:59:43 +00:00
navigation.icon: i-ph-folder-duotone
2021-10-11 12:57:54 +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-10-18 10:59:43 +00:00
:read-more{to="/docs/guide/concepts/auto-imports"}
:link-example{to="/docs/examples/features/auto-imports"}
## Types
Under the hood, Nuxt auto generates the file `.nuxt/imports.d.ts` to declare the types.
Be aware that you have to run [`nuxi prepare` ](/docs/api/commands/prepare ), [`nuxi dev` ](/docs/api/commands/dev ) or [`nuxi build` ](/docs/api/commands/build ) in order to let Nuxt generate the types.
::callout
If you create a composable without having the dev server running, TypeScript will throw an error, such as `Cannot find name 'useBar'.`
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
2023-10-18 10:59:43 +00:00
```bash [Directory Structure]
| composables/
---| index.ts // scanned
---| useFoo.ts // scanned
-----| nested/
-------| utils.ts // not scanned
2022-07-21 14:04:50 +00:00
```
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
]
}
})
```