@nuxt/docs-nightly 4.0.0-29202319.5146bed7 → 4.0.0-29203929.7fe81d4f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -559,6 +559,31 @@ export default defineNuxtModule({
|
|
|
559
559
|
```
|
|
560
560
|
::
|
|
561
561
|
|
|
562
|
+
##### Custom Hooks
|
|
563
|
+
|
|
564
|
+
Modules can also define and call their own hooks, which is a powerful pattern for making your module extensible.
|
|
565
|
+
|
|
566
|
+
If you expect other modules to be able to subscribe to your module's hooks, you should call them in the `modules:done` hook. This ensures that all other modules have had a chance to be set up and register their listeners to your hook during their own `setup` function.
|
|
567
|
+
|
|
568
|
+
```ts
|
|
569
|
+
// my-module/module.ts
|
|
570
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
571
|
+
|
|
572
|
+
export interface ModuleHooks {
|
|
573
|
+
'my-module:custom-hook': (payload: { foo: string }) => void
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export default defineNuxtModule({
|
|
577
|
+
setup (options, nuxt) {
|
|
578
|
+
// Call your hook in `modules:done`
|
|
579
|
+
nuxt.hook('modules:done', async () => {
|
|
580
|
+
const payload = { foo: 'bar' }
|
|
581
|
+
await nuxt.callHook('my-module:custom-hook', payload)
|
|
582
|
+
})
|
|
583
|
+
}
|
|
584
|
+
})
|
|
585
|
+
```
|
|
586
|
+
|
|
562
587
|
#### Adding Templates/Virtual Files
|
|
563
588
|
|
|
564
589
|
If you need to add a virtual file that can be imported into the user's app, you can use the `addTemplate` utility.
|