docs(plugins): add note about composable usage (#6744)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
HomWang 2022-08-22 22:09:12 +08:00 committed by GitHub
parent cf5ba86747
commit 746196f1b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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: