What is @vueuse/core?
The @vueuse/core package is a collection of essential Vue Composition API utilities that help with a wide range of common application needs, from state management to utilities for handling side effects. It's designed to work seamlessly with Vue 3, leveraging the Composition API to make building and managing Vue applications easier and more intuitive.
What are @vueuse/core's main functionalities?
State Management
This feature allows you to easily synchronize a reactive state with localStorage, enabling persistent state across page reloads. The example demonstrates how to store and retrieve user settings.
import { ref } from 'vue'
import { useLocalStorage } from '@vueuse/core'
const userSettings = useLocalStorage('user-settings', { theme: 'dark', notifications: true })
Sensor Utilities
Sensor utilities provide reactive access to browser sensors like mouse position. The example shows how to track the mouse position on the screen.
import { useMouse } from '@vueuse/core'
const { x, y } = useMouse()
Animation
This feature enables easy setup of interval-based side effects, useful for animations or regular data fetching. The code sample demonstrates setting up a simple interval that logs a message every second.
import { useIntervalFn } from '@vueuse/core'
const { pause, resume } = useIntervalFn(() => console.log('Tick'), 1000)
Other packages similar to @vueuse/core
vue-composable
Vue-composable is similar to @vueuse/core, offering a set of composable utilities for Vue. While it covers many of the same areas as @vueuse/core, such as state management and sensor utilities, it has its own unique set of composables and might be preferred for certain use cases.
vuelidate
Vuelidate provides Vue applications with model-based validation. While it focuses specifically on form validation, rather than a broad range of utilities like @vueuse/core, it's a powerful tool for ensuring data integrity in user inputs.
Collection of essential Vue Composition Utilities
ð Features
- ðŠ Interactive docs & demos
- ⥠Fully tree shakeable: Only take what you want, bundle size
- ðĶū Type Strong: Written in TypeScript, with TS Docs
- ð SSR Friendly
- ð No bundler required: Usable via CDN
- ðĐ Flexible: Configurable event filters and targets
- ð Optional Add-ons: Router, Firebase, RxJS, etc.
ðĶ Usage
import { useLocalStorage, useMouse, usePreferredDark } from '@vueuse/core'
export default {
setup() {
const { x, y } = useMouse()
const isDark = usePreferredDark()
const store = useLocalStorage(
'my-storage',
{
name: 'Apple',
color: 'red',
},
)
return { x, y, isDark, store }
},
}
Refer to functions list or documentations for more details.
ðĶ Install
From v12.0, VueUse no longer supports Vue 2. Please use v11.x for Vue 2 support.
npm i @vueuse/core
Add ons | Nuxt Module
Demos
CDN
<script src="https://unpkg.com/@vueuse/shared"></script>
<script src="https://unpkg.com/@vueuse/core"></script>
It will be exposed to global as window.VueUse
ðŠī Project Activity
ð§ą Contribute
See the Contributing Guide
ðļ Thanks
This project is heavily inspired by the following awesome projects.
And thanks to all the contributors on GitHub!
ðĻâð Contributors
Financial Contributors on Open Collective
ð License
MIT License ÂĐ 2019-PRESENT Anthony Fu