From 746196f1b08340bb7fedb5861372a19cff93e705 Mon Sep 17 00:00:00 2001 From: HomWang <516310460@qq.com> Date: Mon, 22 Aug 2022 22:09:12 +0800 Subject: [PATCH] docs(plugins): add note about composable usage (#6744) Co-authored-by: Pooya Parsa --- .../3.directory-structure/11.plugins.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/content/2.guide/3.directory-structure/11.plugins.md b/docs/content/2.guide/3.directory-structure/11.plugins.md index b7676a5467..2b93add1ed 100644 --- a/docs/content/2.guide/3.directory-structure/11.plugins.md +++ b/docs/content/2.guide/3.directory-structure/11.plugins.md @@ -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: