2021-10-11 12:57:54 +00:00
|
|
|
---
|
|
|
|
icon: IconDirectory
|
|
|
|
title: 'composables'
|
|
|
|
head.title: Composables directory
|
|
|
|
---
|
|
|
|
|
|
|
|
# Composables directory
|
|
|
|
|
2021-10-20 09:47:18 +00:00
|
|
|
Nuxt 3 supports `composables/` directory to auto import your Vue composables into your application and use using auto imports!
|
|
|
|
|
|
|
|
|
|
|
|
Example: (using named exports)
|
|
|
|
|
|
|
|
```js [composables/useFoo.ts]
|
|
|
|
import { useState } from '#app'
|
|
|
|
|
2021-10-22 10:17:09 +00:00
|
|
|
export const useFoo = () => {
|
2021-10-20 09:47:18 +00:00
|
|
|
return useState('foo', () => 'bar')
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Example: (using default export)
|
|
|
|
|
|
|
|
```js [composables/use-foo.ts or composables/useFoo.ts]
|
|
|
|
import { useState } from '#app'
|
|
|
|
|
2021-10-21 14:34:54 +00:00
|
|
|
// It will be available as useFoo() (pascalCase of file name without extension)
|
2021-10-20 09:47:18 +00:00
|
|
|
export default function () {
|
2021-10-21 14:34:54 +00:00
|
|
|
return 'bar'
|
2021-10-20 09:47:18 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
You can now auto import it:
|
|
|
|
|
|
|
|
```vue [app.vue]
|
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
{{ foo }}
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
const foo = useFoo()
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
|