VueUse工具库

VueUse

VueUse不是Vue.use,它是为Vue 2和3服务的一套Vue Composition API的常用工具集,是目前世界上Star最高的同类型库之一。它的初衷就是将一切原本并不支持响应式的JS API变得支持响应式,省去程序员自己写相关代码。

VueUse 是一个基于 Composition API 的实用函数集合。通俗的来说,这就是一个工具函数包支持了更好的逻辑分离,它可以帮助你快速实现一些常见的功能,免得你自己去写,解决重复的工作内容。以及进行了机遇 Composition API 的封装。


引入

import { 具体方法 } from ‘@vueuse/core’

一些方法的具体用法

  • useMouse:监听当前鼠标坐标的一个方法,他会实时的获取鼠标的当前的位置
  • useLocalStorage:据持久化到本地存储中
  • throttleFilter:节流 throttleFilter(100)
  • debounceFilter:防抖 debounceFilter(100)
  • OnClickOutside:在点击元素外部时触发一个回调函数
  • **注意:**这里的 OnClickOutside 函数是一个组件,不是一个函数。需要package.json 中安装了 @vueuse/components。
  • useDraggable

在vue中利用useDraggable实现antDesign中的Modal移动

官方示例:

<script setup lang="ts">
import { ref } from 'vue'
import { useDraggable } from '@vueuse/core'
 
const el = ref<HTMLElement | null>(null)
 
// `style` will be a helper computed for `left: ?px; top: ?px;`
const { x, y, style } = useDraggable(el, {
  initialValue: { x: 40, y: 40 },
})
</script>
 
<template>
  <div ref="el" :style="style" style="position: fixed">
    Drag me! I am at {
  {x}}, {
  {y}}
  </div>
</template>

 useDraggable 接收两个参数:target拖拽目标元素。options为可选参数

Component Usage

需要安装  

npm i @vueuse/core @vueuse/components
<UseDraggable :initialValue="{ x: 10, y: 10 }" v-slot="{ x, y }">
  Drag me! I am at {
  {x}}, {
  {y}}
</UseDraggable>

 本地vue2示例

<UseDraggable 
:initialValue="{ x: 10, y: 10 }"  
v-slot="{ x, y }" 
style="cursor: move; position: fixed; z-index: 999; background: red; padding: 12px;"
>
   <div >
    Drag me!I am at {
  { x }}, {
  { y }}
   </div>
</UseDraggable>


import { UseDraggable } from '@vueuse/components'


 components: {
    UseDraggable
 },

data 定义 x y 

 

其他具体可参考官方文档 :VueUse

相关推荐

  1. VueUse工具

    2023-12-10 00:14:02       48 阅读
  2. VueUse使用

    2023-12-10 00:14:02       46 阅读
  3. 我的“工具

    2023-12-10 00:14:02       7 阅读
  4. 全屏解决方案 (screenfull or vueuse)

    2023-12-10 00:14:02       19 阅读
  5. 【置顶】前端工具

    2023-12-10 00:14:02       8 阅读
  6. 软件版本管理工具

    2023-12-10 00:14:02       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-10 00:14:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-10 00:14:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-10 00:14:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-10 00:14:02       20 阅读

热门阅读

  1. string类的常用方法

    2023-12-10 00:14:02       40 阅读
  2. C++知识点总结(8):尺取法

    2023-12-10 00:14:02       33 阅读
  3. go-factory工厂模式样例

    2023-12-10 00:14:02       37 阅读
  4. 【从编译器的角度看多态的底层实现原理】

    2023-12-10 00:14:02       32 阅读
  5. 有限元分析-强度理论

    2023-12-10 00:14:02       37 阅读
  6. 网络函数和文件管理函数

    2023-12-10 00:14:02       30 阅读