Vue新玩具VueUse的具體用法
前言
上次在看前端早早聊大會中, 尤大大再一次提到了 VueUse 的一個庫。 好奇了一下,點看看了看。好家伙啊, 我直接好家伙。這不就是曾經(jīng)我也想自己寫一個 vue 版的 hooks 庫嗎?(因為我覺得 vue3 和 hooks 太像了) 可是我還不太會, 你現(xiàn)在直接把我的夢想給破滅了,下面我們一起來看看吧!VueUse 作者 Anthony Fu 分享可組合的 Vue_嗶哩嗶哩_bilibili
什么是 VueUse
VueUse不是Vue.use,它是為Vue 2和3服務(wù)的一套Vue Composition API的常用工具集,是目前世界上Star最高的同類型庫之一。它的初衷就是將一切原本并不支持響應(yīng)式的JS API變得支持響應(yīng)式,省去程序員自己寫相關(guān)代碼。
VueUse 是一個基于 Composition API 的實用函數(shù)集合。通俗的來說,這就是一個工具函數(shù)包,它可以幫助你快速實現(xiàn)一些常見的功能,免得你自己去寫,解決重復(fù)的工作內(nèi)容。以及進行了機遇 Composition API 的封裝。讓你在 vue3 中更加得心應(yīng)手。
簡單上手
安裝 VueUse
npm i @vueuse/core
使用 VueUse
// 導(dǎo)入 import { useMouse, usePreferredDark, useLocalStorage } from '@vueuse/core' export default { setup() { // tracks mouse position const { x, y } = useMouse() // is user prefers dark theme const isDark = usePreferredDark() // persist state in localStorage const store = useLocalStorage( 'my-storage', { name: 'Apple', color: 'red', }, ) return { x, y, isDark, store } } }
上面從 VueUse 當(dāng)中導(dǎo)入了三個函數(shù), useMouse, usePreferredDark, useLocalStorage。useMouse 是一個監(jiān)聽當(dāng)前鼠標坐標的一個方法,他會實時的獲取鼠標的當(dāng)前的位置。usePreferredDark 是一個判斷用戶是否喜歡深色的方法,他會實時的判斷用戶是否喜歡深色的主題。useLocalStorage 是一個用來持久化數(shù)據(jù)的方法,他會把數(shù)據(jù)持久化到本地存儲中。
還有我們熟悉的 防抖 和 節(jié)流
import { throttleFilter, debounceFilter, useLocalStorage, useMouse } from '@vueuse/core' // 以節(jié)流的方式去改變 localStorage 的值 const storage = useLocalStorage('my-key', { foo: 'bar' }, { eventFilter: throttleFilter(1000) }) // 100ms后更新鼠標的位置 const { x, y } = useMouse({ eventFilter: debounceFilter(100) })
還有還有在 component 中使用的函數(shù)
<script setup> import { ref } from 'vue' import { onClickOutside } from '@vueuse/core' const el = ref() function close () { /* ... */ } onClickOutside(el, close) </script> <template> <div ref="el"> Click Outside of Me </div> </template>
上面例子中,使用了 onClickOutside 函數(shù),這個函數(shù)會在點擊元素外部時觸發(fā)一個回調(diào)函數(shù)。也就是這里的 close 函數(shù)。在 component 中就是這么使用
<script setup> import { OnClickOutside } from '@vueuse/components' function close () { /* ... */ } </script> <template> <OnClickOutside @trigger="close"> <div> Click Outside of Me </div> </OnClickOutside> </template>
注意⚠️ 這里的 OnClickOutside 函數(shù)是一個組件,不是一個函數(shù)。需要package.json 中安裝了 @vueuse/components。
還還有全局狀態(tài)共享的函數(shù)
// store.js import { createGlobalState, useStorage } from '@vueuse/core' export const useGlobalState = createGlobalState( () => useStorage('vue-use-local-storage'), )
// component.js import { useGlobalState } from './store' export default defineComponent({ setup() { const state = useGlobalState() return { state } }, })
這樣子就是一個簡單的狀態(tài)共享了。擴展一下。傳一個參數(shù),就能改變 store 的值了。
還有關(guān)于 fetch, 下面👇就是一個簡單的請求了。
import { useFetch } from '@vueuse/core' const { isFetching, error, data } = useFetch(url)
它還有很多的 option 參數(shù),可以自定義。
// 100ms超時 const { data } = useFetch(url, { timeout: 100 })
// 請求攔截 const { data } = useFetch(url, { async beforeFetch({ url, options, cancel }) { const myToken = await getMyToken() if (!myToken) cancel() options.headers = { ...options.headers, Authorization: `Bearer ${myToken}`, } return { options } } }) // 響應(yīng)攔截 const { data } = useFetch(url, { afterFetch(ctx) { if (ctx.data.title === 'HxH') ctx.data.title = 'Hunter x Hunter' // Modifies the resposne data return ctx }, })
更多
到此這篇關(guān)于Vue新玩具VueUse的具體用法的文章就介紹到這了,更多相關(guān)Vue VueUse內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Vite搭建Vue3+ElementUI-Plus項目的全過程
vue3如今已經(jīng)成為默認版本了,相信大多數(shù)公司已經(jīng)全面擁抱vue3了,下面這篇文章主要給大家介紹了關(guān)于利用Vite搭建Vue3+ElementUI-Plus項目的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07vue-cli項目中使用公用的提示彈層tips或加載loading組件實例詳解
這篇文章主要介紹了vue-cli項目中使用公用的提示彈層tips或加載loading組件,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2018-05-05electron實現(xiàn)打印功能支持靜默打印、無感打印
使用electron開發(fā)應(yīng)用遇到了打印小票的功能,實現(xiàn)途中還是幾經(jīng)波折,下面這篇文章主要給大家介紹了關(guān)于electron實現(xiàn)打印功能支持靜默打印、無感打印的相關(guān)資料,需要的朋友可以參考下2023-12-12element-plus按需引入后ElMessage與ElLoading在頁面中的使用
這篇文章主要介紹了element-plus按需引入后ElMessage與ElLoading在頁面中的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09