Vue Global API
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
-
ESLint: 'ref' is not defined. (no-undef)
cause ESLint is not aware that `ref` (or other global APIs) are globally available through `vue-global-api`.fixAdd `'vue-global-api'` to the `extends` array in your `.eslintrc.js` (or `.eslintrc.cjs`) configuration file. For example: `extends: ['plugin:vue/vue3-recommended', 'vue-global-api']`. -
TypeError: ref is not a function
cause The `vue-global-api` package was either not imported, or it was imported *after* a component that attempted to use a global API.fixEnsure `import 'vue-global-api'` is present in your application's entry file (e.g., `main.ts`) and that it executes *before* any Vue components are initialized. Verify Vue 3 is correctly installed and configured. -
Module not found: Error: Can't resolve 'vue-global-api'
cause The `vue-global-api` package is not installed or incorrectly referenced in `package.json`.fixInstall the package using `npm install vue-global-api` or `yarn add vue-global-api`. Check `package.json` to confirm it's listed in `dependencies`.
Warnings
- gotcha Failing to configure ESLint will result in 'no-undef' errors for globally exposed APIs.
- gotcha The global registration import (`import 'vue-global-api'`) must occur *before* any Vue components are processed that utilize these global APIs.
- gotcha This package is not designed for direct CDN usage. For browser environments without a bundler, the recommended approach is to manually assign Vue's global object to `window` (e.g., `Object.assign(window, Vue)`).
- gotcha Do not attempt to import named exports directly from `vue-global-api` or its submodules (e.g., `import { ref } from 'vue-global-api'`). The package works by side-effect global registration.
Install
-
npm install vue-global-api -
yarn add vue-global-api -
pnpm add vue-global-api
Imports
- Global Registration
import { ref } from 'vue-global-api'import 'vue-global-api'
- Specific API Registration
const ref = require('vue-global-api/ref')import 'vue-global-api/ref'
- Collection Registration
import { watch } from 'vue-global-api/reactivity'import 'vue-global-api/reactivity'
Quickstart
/* 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
}