目录引言使用前安装网页全屏剪切板取色器拖拽元素本地缓存其他安全区域动态修改favicon引言 VueUse是一个基于Composition api的实用函数集合,支持Vue2和vue
VueUse
是一个基于Composition api
的实用函数集合,支持Vue2
和vue3
,使用它可以帮助我们快速实现日常开发中一些常见的需求。本文将分享列举几个常见的需求来通过VueUse
实现,让大家感受其魅力!
Vue3:
npm i @vueuse/core --save
Vue2 的话还需要额外安装 @vue/composition-api
npm i @vue/composition-api --save
在后台管理系统中,往往都有一个开启网页全屏的功能,大部分都是使用screenfull
插件实现的。
VueUse
里为我们提供了相关的API,让我们可以轻松的实现网页全屏。
<template>
<el-button @click="toggle">
{{ isFullscreen ? '退出全屏' : '开启全屏' }}
</el-button>
</template>
<script lang="ts" setup>
import { useFullscreen } from '@vueuse/core'
const { isFullscreen, toggle } = useFullscreen()
</script>
useFullscreen
也支持传入某个元素,这样只会对该元素区域进行全屏显示。
<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
插件来实现的,同样的,用VueUse
也可以轻松实现。
<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('我是被复制的内容').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>
调用open
函数即可打开取色器,在任意地方点击鼠标左键即可响应式得到颜色。
<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 的转换。
接着我们便可以很方便的操作对象里的某一个字段,而不需要我们使用原生API那样取出一整个对象再进行替换,可以说是非常令人舒适了。
state.value.id == 'abc' // 查看localStorage可以发现id被更改为abc
使用sessionStorage
方式:
const state = useStorage('test', { id: 'xxxx', name: 'james' }, sessionStorage)
使用useScreenSafeArea
可以轻松获得屏幕的安全区域距离,再也不用担心刘海屏和底部安全距离了。
<script lang="ts" setup>
import { useScreenSafeArea } from '@vueuse/core'
const { top, right, bottom, left } = useScreenSafeArea()
</script>
如果在项目里需要我们去动态修改favicon
,创建标签、添加元素、替换地址等等操作,虽然代码量也不是很多,但显然用下面的方式要方便得多了。
<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>
如上,我们可以动态的将一张图片设置为网站的icon。
以上就是VueUse功能精简你的dependencies的详细内容,更多关于VueUse精简dependencies的资料请关注编程网其它相关文章!
--结束END--
本文标题: VueUse功能精简你的dependencies
本文链接: https://www.lsjlt.com/news/153757.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0