Skip to content

Commit

Permalink
docs(plugins): add note about composable usage (nuxt#6744)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
HomWang and pi0 authored Aug 22, 2022
1 parent cf5ba86 commit 746196f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/content/2.guide/3.directory-structure/11.plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ export default defineNuxtPlugin(nuxtApp => {
})
```

## Using Composables Within Plugins

You can use [composables](/guide/directory-structure/composables) within Nuxt plugins:

```ts
export default defineNuxtPlugin((NuxtApp) => {
const foo = useFoo()
})
```

However, keep in mind there are some limitations and differences:

**If a composable depends on another plugin registered later, it might not work.**

**Reason:** Plugins are called in order sequencially and before everything else. You might use a composable that dependants on another plugin which is not called yet.

**If a composable depends on the Vue.js lifecycle, it won't work.**

**Reason:** Normally, Vue.js composables are bound to the current component instance while plugins are only bound to `nuxtApp` instance.

## Automatically Providing Helpers

If you would like to provide a helper on the `NuxtApp` instance, return it from the plugin under a `provide` key. For example:
Expand Down

0 comments on commit 746196f

Please sign in to comment.