VueUse功能精簡你的dependencies
引言
VueUse是一個基于Composition API的實用函數(shù)集合,支持Vue2和Vue3,使用它可以幫助我們快速實現(xiàn)日常開發(fā)中一些常見的需求。本文將分享列舉幾個常見的需求來通過VueUse實現(xiàn),讓大家感受其魅力!
使用前安裝
Vue3:
npm i @vueuse/core --save
Vue2 的話還需要額外安裝 @vue/composition-api
npm i @vue/composition-api --save
網(wǎng)頁全屏
在后臺管理系統(tǒng)中,往往都有一個開啟網(wǎng)頁全屏的功能,大部分都是使用screenfull插件實現(xiàn)的。
VueUse里為我們提供了相關(guān)的API,讓我們可以輕松的實現(xiàn)網(wǎng)頁全屏。
<template>
<el-button @click="toggle">
{{ isFullscreen ? '退出全屏' : '開啟全屏' }}
</el-button>
</template>
<script lang="ts" setup>
import { useFullscreen } from '@vueuse/core'
const { isFullscreen, toggle } = useFullscreen()
</script>
useFullscreen也支持傳入某個元素,這樣只會對該元素區(qū)域進行全屏顯示。
<template>
<el-button @click="toggle">
開啟全屏
</el-button>
<div ref="el">把我全屏</div>
</template>
<script lang="ts" setup>
import { useFullscreen } from '@vueuse/core'
const el = ref<HTMLElement | null>(null)
const { toggle } = useFullscreen(el)
</script>
剪切板
以前在Vue2里都是用vue-clipboard2插件來實現(xiàn)的,同樣的,用VueUse也可以輕松實現(xiàn)。
<template>
<el-button @click="onClick">copy</el-button>
</template>
<script lang="ts" setup>
import { useClipboard } from '@vueuse/core'
const { isSupported, copy } = useClipboard()
const onClick = () => {
if (isSupported) {
copy('我是被復制的內(nèi)容').then(() => {
console.log('copy success')
})
} else {
alert('Your browser does not support Clipboard API')
}
}
</script>
取色器

<template>
<div>
<el-button @click="open">打開取色器</el-button>
<el-button type="primary" style="width: 100px">按鈕</el-button>
<p>顏色:{{ sRGBHex }}</p>
</div>
</template>
<script lang="ts" setup>
import { useEyeDropper } from '@vueuse/core'
const { open, sRGBHex } = useEyeDropper()
</script>
調(diào)用open函數(shù)即可打開取色器,在任意地方點擊鼠標左鍵即可響應式得到顏色。
拖拽元素

<template>
<div
ref="el"
style="position: fixed; width: 400px; height: 400px; background: red"
:style="style"
></div>
<p>x: {{ x }},y:{{ y }}</p>
</template>
<script lang="ts" setup>
import { useDraggable } from '@vueuse/core'
const el = ref<HTMLElement | null>(null)
const { x, y, style } = useDraggable(el)
</script>
簡單的幾行代碼就能讓元素可拖拽。
本地緩存
<script lang="ts" setup>
import { useStorage } from '@vueuse/core'
const state = useStorage('test', { id: 'xxxx', name: 'james' })
</script>
上面的代碼會以test作為key存入一個對象,返回值是一個ref類型。
該操作可以讓我們不用像使用原生API一樣進行 json to string 的轉(zhuǎn)換。
接著我們便可以很方便的操作對象里的某一個字段,而不需要我們使用原生API那樣取出一整個對象再進行替換,可以說是非常令人舒適了。
state.value.id == 'abc' // 查看localStorage可以發(fā)現(xiàn)id被更改為abc
使用sessionStorage方式:
const state = useStorage('test', { id: 'xxxx', name: 'james' }, sessionStorage)
其他
安全區(qū)域
使用useScreenSafeArea可以輕松獲得屏幕的安全區(qū)域距離,再也不用擔心劉海屏和底部安全距離了。

<script lang="ts" setup>
import { useScreenSafeArea } from '@vueuse/core'
const { top, right, bottom, left } = useScreenSafeArea()
</script>
動態(tài)修改favicon
如果在項目里需要我們?nèi)討B(tài)修改favicon,創(chuàng)建標簽、添加元素、替換地址等等操作,雖然代碼量也不是很多,但顯然用下面的方式要方便得多了。
<template>
<el-button @click="onClick">切換favicon</el-button>
</template>
<script lang="ts" setup>
import { useFavicon } from '@vueuse/core'
import Logo from '@/assets/image/logo.png'
const icon = useFavicon()
const onClick = () => {
icon.value = Logo
}
</script>
如上,我們可以動態(tài)的將一張圖片設(shè)置為網(wǎng)站的icon。
以上就是VueUse功能精簡你的dependencies的詳細內(nèi)容,更多關(guān)于VueUse精簡dependencies的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題
這篇文章主要介紹了VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue+axios實現(xiàn)圖片上傳識別人臉的示例代碼
本文主要介紹了vue+axios實現(xiàn)圖片上傳識別人臉,這里采用的是vant的文件上傳組件,通過上傳圖片后端識別圖片里的人臉,感興趣的可以了解一下2021-11-11
vue 解決form表單提交但不跳轉(zhuǎn)頁面的問題
今天小編就為大家分享一篇vue 解決form表單提交但不跳轉(zhuǎn)頁面的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

