Vue Global Config
Vue Global Config is a utility library for Vue 2 and Vue 3 applications that enables global configuration of component properties, attributes, listeners, hooks, and slots. It addresses a common need in Vue development where components registered globally lack a straightforward way to accept global configuration. The library provides a `createGlobalConfig` function to register a configuration plugin with `app.use()` (Vue 3) or `Vue.use()` (Vue 2), and a `ConfigProvider` component for declarative configuration. It supports the current stable Vue 3.x and Vue 2.x versions, often releasing new patches to support the latest Vue minor versions or fix integration issues. Its key differentiator is offering a generic global configuration mechanism for *any* component, unlike framework-specific solutions (e.g., Element Plus's ConfigProvider) which are often limited to components within their own ecosystem. The package is currently at version 0.6.3 and actively maintained.
Common errors
-
TypeError: app.use expects an install function or an object with an install method
cause Attempting to pass a raw Vue component directly to `app.use()` (or `Vue.use()` in Vue 2) instead of the plugin returned by `createGlobalConfig`.fixWrap your component and global configuration with `createGlobalConfig`: `app.use(createGlobalConfig(YourComponent, { ... }))`. -
Property 'someProp' was accessed during render but is not defined on instance.
cause A global prop was defined but the target component does not declare it in its `props` option, or there's a mismatch in naming convention (e.g., camelCase vs. kebab-case).fixEnsure the target component explicitly declares all global props it intends to consume in its `props` option. Check `options.camelizePropNames` in `createGlobalConfig` options for naming consistency. -
The @vnode-mounted hook is deprecated in Vue 3.4 and later and will not function as expected.
cause Using the `@vnode-mounted` or `@vnodeMounted` hook syntax for global component hooks in a Vue 3.4+ environment.fixUpdate your global configuration to remove these deprecated hooks, as they are no longer supported in Vue 3.4 and later.
Warnings
- breaking The support for `@vnode-mounted` and `@vnodeMounted` hooks was removed in Vue 3.4. Applications upgrading to Vue 3.4 or later using these specific global hooks will need to refactor them.
- breaking The global `props` option structure changed significantly. `props` were moved into `options.props`, and a new `options.camelizePropNames` option was introduced to control prop name camelization.
- gotcha The README examples for `app.use(YourComponent, config)` are misleading. The `createGlobalConfig` function must be explicitly called to generate the plugin, i.e., `app.use(createGlobalConfig(YourComponent, config))`.
- gotcha Vue 2 and Vue 3 use different naming conventions for component lifecycle hooks and event listeners. For instance, Vue 2 uses `@hook:mounted` while Vue 3 uses `@vue:mounted` for component hooks.
Install
-
npm install vue-global-config -
yarn add vue-global-config -
pnpm add vue-global-config
Imports
- createGlobalConfig
const createGlobalConfig = require('vue-global-config');import { createGlobalConfig } from 'vue-global-config'; - ConfigProvider
const { ConfigProvider } = require('vue-global-config');import { ConfigProvider } from 'vue-global-config'; - ConfigOptions
import type { ConfigOptions } from 'vue-global-config';
Quickstart
import { createApp, h, defineComponent } from 'vue';
import { createGlobalConfig } from 'vue-global-config';
// Define a sample Vue component that can receive global configurations
const MyButton = defineComponent({
props: { title: String, data: Array },
emits: ['leftCheckChange'],
mounted() {
console.log('Component Mounted:', this.title);
// Trigger a global listener via emit
this.$emit('leftCheckChange');
},
render() {
// Access default slot, potentially from global configuration
const defaultSlot = this.$slots.default ? this.$slots.default({ option: { label: 'Default Slot Item' } }) : 'Default Content';
return h('button',
{
// Global attributes can be accessed via $attrs
onClick: () => console.log('Button clicked'),
'data-custom': this.$attrs['data-custom']
},
[
// Render global prop if available, otherwise fallback
h('span', this.title || 'No Title'),
// Render global data if available
this.data?.map(item => h('li', item.label)),
h('div', defaultSlot)
]
);
},
});
const app = createApp({
template: `
<div id="app">
<!-- MyButton without local props, relying on global config -->
<MyButton />
<!-- MyButton with local prop, overriding global config for 'title' -->
<MyButton title="Override Title" />
</div>
`,
});
// Apply global configuration to MyButton using the createGlobalConfig plugin
app.use(createGlobalConfig(MyButton, {
// Global Prop: 'title' will be passed as a prop to MyButton instances
'title': 'Global Button Title',
// Global Attr: 'data-custom' will be available via $attrs on MyButton instances
'data-custom': 'Global Custom Data',
// Global Listener: '@leftCheckChange' will be triggered when MyButton emits it
'@leftCheckChange': function () {
console.log('Global LeftCheckChange triggered by MyButton');
},
// Global Hook: '@vue:mounted' will run when any MyButton instance mounts
'@vue:mounted': function () {
console.log('Global Mounted hook for MyButton');
},
// Global Scoped Slot: A default scoped slot provided globally
'#default': ({ option }) => h('em', undefined, `${option.label} (From Global Scoped Slot)`)
}));
app.mount('#app');