Vue Global API

0.4.1 · active · verified Sun Apr 19

vue-global-api is a utility package for Vue 3 that makes Composition API functions globally available within `<script setup>` contexts, removing the need for explicit imports of `ref`, `computed`, `watch`, and other common APIs from 'vue'. This aims to reduce boilerplate and streamline the development experience, aligning with the macro-like availability of `defineProps` and `defineEmits`. The current stable version is `0.4.1`, and it's maintained by Anthony Fu, a prominent Vue core team member. While Vue itself doesn't have a fixed release cycle for minor versions (typically 3-6 months), this package tends to follow its development. Key differentiators include its granular control over which APIs are global via sub-module imports (e.g., `vue-global-api/ref` or `vue-global-api/reactivity`), and its specific ESLint configuration to prevent 'no-undef' errors. It is designed for bundler-based Vue applications and offers an alternative to importing APIs from 'vue' in every single-file component.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core usage of `vue-global-api` by registering all common Composition APIs globally in `main.ts` and then using them directly in a Vue component without explicit imports. It also includes the necessary ESLint configuration.

/* main.ts */
import { createApp } from 'vue'
import App from './App.vue'

// Globally register all common Vue Composition APIs
// This must be imported BEFORE any Vue components that use these globals
import 'vue-global-api'

createApp(App).mount('#app')

/* App.vue */
<script setup lang="ts">
// No need to import ref, computed, watch, etc.
// They are globally available thanks to 'vue-global-api'
const counter = ref(0)

const doubled = computed(() => counter.value * 2)

watch(doubled, (v) => {
  console.log('Doubled value changed:', v)
})

function increment() {
  counter.value++
}
</script>

<template>
  <div>
    <h1>Counter: {{ counter }}</h1>
    <h2>Doubled: {{ doubled }}</h2>
    <button @click="increment">Increment</button>
  </div>
</template>

<style scoped>
/* Your styles here */
</style>

/* .eslintrc.js (or .eslintrc.cjs) */
module.exports = {
  extends: [
    'eslint:recommended',
    'plugin:vue/vue3-recommended',
    'vue-global-api' // Extend the ESLint config for global APIs
    // Or for fine-grain control:
    // 'vue-global-api/reactivity'
  ]
  // ... other ESLint configurations
}

view raw JSON →