Vue Global Config

0.6.3 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates applying global props, attributes, listeners, hooks, and a scoped slot to a `MyButton` component using `createGlobalConfig` in a Vue 3 application. It includes both default and overridden component usage.

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');

view raw JSON →