Overview
Factory for creating Vue plugins that safely provide dependency injection contexts at the application level. Wraps the provide function inapp.runWithContext() to ensure proper execution context.
Also exports createPluginContext — a higher-level factory that generates the standard context/plugin/consumer triple for plugin composables, eliminating boilerplate.
Signature
createPlugin
Creates a Vue plugin with proper context handling.Parameters
Unique identifier for the plugin. Used to prevent duplicate installation.
Function that provides contexts to the Vue app. Called during plugin installation within
app.runWithContext().Optional setup callback executed once per app after context provision. Use for adapter initialization, global mixins, or side effects.
Return Value
A Vue plugin object with an
install method. The plugin ensures:- Context provision runs in proper execution context
- Setup callback runs only once per app (duplicate installation guard)
- Provide always runs, setup guarded by namespace
Usage
createPluginContext
Higher-level factory that generates the standard context/plugin/consumer triple pattern used throughout Vuetify Zero.Parameters
Default dependency injection namespace (e.g.,
'v0:logger'). Can be overridden via options.Function that creates the composable context instance from options.
Optional setup callback invoked once per app after context provision. Receives the context instance, app, and options.
Optional fallback factory. When provided, the generated
useX consumer uses the defensive pattern: returns the fallback when called outside a component instance or when context is not found.Required for composables that may be consumed outside component setup (e.g., useLogger, useLocale).Return Value
A readonly tuple containing three functions:
Creates a context trinity:
[useContext, provideContext, contextInstance].See createTrinity for details on the trinity pattern.Creates a Vue plugin that provides the context at app level.
Consumer function that retrieves the context. Falls back if
fallback config is provided and no injection context exists.Usage
Best Practices
Use createPluginContext for plugin composables
Use createPluginContext for plugin composables
When creating a new composable that will be installed as a plugin, prefer
createPluginContext over manually wiring up createContext, createPlugin, and createTrinity.Provide fallback for non-component usage
Provide fallback for non-component usage
If your composable may be called outside component setup (utility functions, middleware, initialization code), provide a
fallback to prevent injection errors:Keep setup side-effect free
Keep setup side-effect free
The
setup callback should be idempotent and avoid global side effects that could cause issues if the plugin is accidentally installed twice.Use unique namespaces
Use unique namespaces
Always use the
v0: prefix for Vuetify Zero plugins, and ensure namespace uniqueness to prevent collisions:Type Safety
Related
createContext
Low-level context creation
createTrinity
Trinity pattern for context composables