GitHub - antfu/vue-global-api: Use Vue Composition API globally (original) (raw)

✨ Try unplugin-auto-import, a compile time successor of this package. Providing a lot more flexibility and tree-shaking support!


vue-global-api

Use Vue Composition API globally

Instead of import APIs from vue in every file,

Now you can directly use them everywhere (with TypeScript support!)

Installation

Then import vue-global-api in your main entry to register the global apis before any usages

// main.js import 'vue-global-api'

And enjoy :)

Powered by vue-demi, this package also works for Vue 2.

Customization

By default, importing vue-global-api will register all common composition apis to the global object. If you want to have fine-grain control, use submodule per API:

// only register ref and computed as global APIs import 'vue-global-api/ref' import 'vue-global-api/computed'

Collections

We have provided a few sub-set collections if you want to control the global API registration by their type.

// register all reactivity apis (ref, computed, watch, etc.) import 'vue-global-api/reactivity' // register all lifecycle hooks (onMounted, onUpdated, onUnmounted, etc.) import 'vue-global-api/lifecycle' // register component apis (inject, provide, h, etc.) import 'vue-global-api/component'

CDN Usage

If you want to have global api work in CDN, you don't actually need this package. All you need to do is:

Object.assign(window, Vue)

Motivation

In the latest