Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

microcipcip/vue-use-kit

Repository files navigation



Vue use kit


NPM Version NPM Downloads Build Status GitHub license Demo

🛠️ Vue kit of useful Vue Composition API functions.

Install

npm install vue-use-kit

Since Vue 3.0 has not yet been released, you must also install @vue/composition-api library, which will enable the composition API in Vue 2.0.

npm install @vue/composition-api

Setup

import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)

Usage

<template>
 <div>isDesktop: {{ isDesktop ? 'Yes' : 'No' }}</div>
</template>
<script lang="ts">
 import Vue from 'vue'
 import { useMedia } from 'vue-use-kit'
 export default Vue.extend({
 name: 'UseMedia',
 setup() {
 const query = '(min-width: 1024px)'
 const isDesktop = useMedia(query)
 return { isDesktop }
 }
 })
</script>

APIs

🎁 Other examples of composition API functions

Some Vue composition API functions have not been included in this library but can be created easily by following the steps below.

useStore

Creating a useStore function connected to Vuex store is pretty straightforward. For example, given the following store:

// @src/mystore.ts
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
 state: { searchTerm: '' },
 mutations: {
 SET_SEARCH(state, newVal) {
 state.searchTerm = newVal
 }
 },
 getters: { searchTerm: state => state.searchTerm },
 actions: {},
 modules: {}
})
export default store

We can get the store from the vm and expose it in our useStore function:

// @src/useStore.ts
import { getCurrentInstance } from '@vue/composition-api'
export function useStore() {
 const vm = getCurrentInstance()
 if (!vm) throw new Error('Vue instance not found!')
 return vm.$store
}

Now we can use useStore inside the setup() method of our component:

// MyComponent.vue
<template>
 <input type="text" v-model="searchTerm" placeholder="🔎 Search..." />
</template>
<script lang="ts">
 import Vue from 'vue'
 import { ref, watch } from '@src/api'
 import { useStore } from '@src/useStore'
 export default Vue.extend({
 name: 'UseStoreDemo',
 setup() {
 const { commit, getters } = useStore()
 const searchTerm = ref(getters['searchTerm'])
 watch(searchTerm, newVal => commit('SET_SEARCH', newVal))
 return { searchTerm }
 }
 })
</script>
useRouter

Creating a useRouter function connected to VueRouter is rather simple. We can get $route and $router from the vm and expose them in our useRouter function:

// @src/useRouter.ts
import { getCurrentInstance } from '@vue/composition-api'
export function useRouter() {
 const vm = getCurrentInstance()
 if (!vm) throw new Error('Vue instance not found!')
 const route = vm.$route
 const router = vm.$router
 return { route, router }
}

Now we can use useRouter inside the setup() method of our component:

// MyComponent.vue
<template>
 <div>
 Current id: {{ id }}
 </div>
</template>
<script lang="ts">
 import Vue from 'vue'
 import { useRouter } from '@src/useRouter'
 export default Vue.extend({
 name: 'UseRouterDemo',
 setup() {
 const { route } = useRouter()
 return { id: route.params.id }
 }
 })
</script>

Inspiration

Made with

About

🛠️Useful collection of Vue composition API functions https://microcipcip.github.io/vue-use-kit/

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /