Skip to content
프로젝트

소개

Use Vue Composition API globally

기타 프로젝트

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

✨ 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,

<script setup>
import { ref, computed, watch } from 'vue'

const counter = ref(0)

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

watch(doubled, (v) => {
  console.log('New value: ' + v)
})
</script>

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

<script setup>
const counter = ref(0)

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

watch(doubled, (v) => {
  console.log('New value: ' + v)
})
</script>

Installation

npm i vue-global-api

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 <script setup>, compile time macros like defineProps and defineEmits are now available globally without the need to import them from vue. So, as your components are likely to rely on composition APIs like ref and computed, why don't we just have them available globally as well?

ESLint

If you use ESLint it'll complain about you using undefined variables. This package provides the ESLint config presets to solve it.

Extend in your ESLint config:

// .eslintrc.js
module.exports = {
  extends: [
    'vue-global-api'
  ]
};

It also provides the same collections and single API options for fine-grain control.

// .eslintrc.js
module.exports = {
  extends: [
    // collections
    'vue-global-api/reactivity',
    'vue-global-api/lifecycle',
    'vue-global-api/component',
    // single apis
    'vue-global-api/ref',
    'vue-global-api/toRef',
  ]
};

License

MIT License © 2021 Anthony Fu

관련 프로젝트 살펴보기

불러오는 중…