Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
docs(composables): add nested and plugin injection examples (#6743)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
HomWang and pi0 authored Aug 22, 2022
1 parent dc6b6ce commit 39a6054
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion docs/content/2.guide/3.directory-structure/5.composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Under the hood, Nuxt auto generates the file `.nuxt/auto-imports.d.ts` to declar

Be aware that you have to run `nuxi prepare`, `nuxi dev` or `nuxi build` in order to let Nuxt generates the types. If you create a composable without having the dev server running, typescript will throw an error `Cannot find name 'useBar'.`

## Example
## Usage

**Method 1:** Using named export

Expand Down Expand Up @@ -47,6 +47,30 @@ const foo = useFoo()

:LinkExample{link="/examples/auto-imports/composables"}

## 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

You can access [plugin injections](/guide/directory-structure/plugins#automatically-providing-helpers) from composables:

```js [composables/test.ts]
export const useHello = () => {
const nuxtApp = useNuxtApp()
return nuxtApp.$hello
}
```

## How Files Are Scanned

Nuxt only scans files at the top level of the `composables/` directory, e.g.:
Expand Down

0 comments on commit 39a6054

Please sign in to comment.