Skip to main content

Context

Nuxt Kit provides a set of utilities to help you work with context. These functions enable you to conveniently access the Nuxt instance from the context without having to pass it as an argument. Nuxt modules allow you to enhance Nuxt’s capabilities. They offer a structured way to keep your code organized and modular.

useNuxt

Get the Nuxt instance from the context. It will throw an error if Nuxt is not available.

Type

Return Value

The useNuxt function returns the Nuxt instance with the following key properties:

Usage

Example: Setting up transpilation

tryUseNuxt

Get the Nuxt instance from the context. It will return null if Nuxt is not available.

Type

Return Value

The tryUseNuxt function returns the Nuxt instance if available, or null if Nuxt is not available. The Nuxt instance has the same properties as described in the useNuxt section.

Usage

Example: Requiring site config

When to use context functions

Note: When you’re working with the setup function in Nuxt modules, Nuxt is already provided as the second argument. This means you can access it directly without needing to call useNuxt().
Use useNuxt() and tryUseNuxt() when you’re breaking down your module into smaller components that don’t receive the Nuxt instance as a parameter.

Source

View source on GitHub