Guoguo-notes
主页
  • 常用笔记
  • 飞码篇
  • Java
  • React笔记
  • 袁果锅生态
GitHub
主页
  • 常用笔记
  • 飞码篇
  • Java
  • React笔记
  • 袁果锅生态
GitHub
  • 常用笔记

    • CSS 封装.md
    • CSS笔记.md
    • Echarts.md
    • Element Plus --vue3.0.md
    • Element ui 笔记.md
    • Git 代码管理.md
    • Html 笔记.md
    • Taro.md
    • TypeScript.md
    • Vue 代码片段.md
    • Vue 全局封装 main.js.md
    • Vue 笔记.md
    • Vue3 .md
    • Vue3+Element Plus.md
    • axios 请求拦截.md
    • ecahrts 使用地图报错问题.md
    • element plus 本地启动.md
    • jsx tsx 代码片段.md
    • jsx tsx 笔记.md
    • js常规.md
    • npm.md
    • sh 笔记.md
    • uniapp笔记.md
    • valibot校验学习.md
    • vite 笔记.md
    • vite手写插件.md
    • vue.js 下载文件.md
    • vueuse笔记.md
    • vxe-table笔记.md
    • 云开发教程.md
    • 公共API接口.md
    • 小程序笔记.md
    • 常用插件.md
    • 插件库.md
    • 服务器.md
    • 服务器部署教学.md
    • 毕业设计接单.md
    • 汇智腾远笔记.md
    • 浏览器px to rem适配.md
    • 登录流程.md
    • 登录逻辑.md
    • 网站配色.md
    • 视频播放插件.md
    • 解析数学公式.md
    • 跨域代理.md

vueuse --> @vueuse/core

安装

npm i @vueuse/core

useDebounceFn(防抖)

防抖是指当一个事件被触发后,在一定时间内如果该事件再次被触发,那么就会取消前一次的操作,重新开始计时。(防止在短时间内频繁触发事件导致页面性能下降)

<template>
  <div>
    <div>{{ counter }}</div>
    <button @click="addNum">add</button>
  </div>
</template>
<script lang="ts" setup>
import { useDebounceFn } from "@vueuse/core";
const counter = ref(0);
const handleAddFun = () => {
  counter.value += 1;
};
const addNum = useDebounceFn(handleAddFun, 300, { maxWait: 5000 });
</script>

useFullscreen(全屏)

<template>
  <button @click="toggle">全屏</button>
</template>
<script lang="ts" setup>
import { useFullscreen } from '@vueuse/core'
const { isFullscreen, toggle } = useFullscreen()
console.log('isFullscreen ===>', isFullscreen)
</script>

useClipboard (剪切板功能)

<template>
 <div>
   <input v-model="input" type="text" />
   <button @click="copy(input)">Copy</button>
 </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { useClipboard } from "@vueuse/core";
const input = ref("");
const { text, copy, isSupported } = useClipboard();
// text: 复制的内容; copy: 复制函数; isSupported: 当前的浏览器是否支持这个api
</script>

useColorMode(切换主题颜色)

<template>
	<div>
	  <div :class="mode" style="width: 40px; height: 40px"></div>
      <button @click="btn">切换主题色</button>
	</div>
</template>
<script lang="ts" setup>
import { useColorMode } from "@vueuse/core";
const mode = useColorMode();
const btn = () => {
  mode.value = mode.value === "light" ? "dark" : "light";
};
</script>
<style>
.light {
  background-color: #ccc;
}
.dark {
  background-color: #333;
}
</style>

useWindowSize(响应式获取窗口尺寸)

<template>
	<div>
	  <div>{{ width }}-{{ height }}</div>
	</div>
</template>
<script lang="ts" setup>
import { useWindowSize } from "@vueuse/core";
const { width, height } = useWindowSize();
</script>

onClickOutside 检测区域之外的的点击事件

<script setup>
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'

const container = ref(null)
onClickOutside(container, () => alert('Good. Better to click outside.'))
</script>

<template>
  <div>
   <p>Hey there, here's some text.</p>
    <div style="background-color: pink;;" ref="container">
      <p>Please don't click in here.</p>
    </div>
  </div>
</template>

useStorage 自动同步到 localstorage

useStorage真的很酷,因为它会自动将 ref 同步到 localstorage,事例如下:

<script setup>
import { useStorage } from '@vueuse/core'
const input = useStorage('unique-key', 'Hello, world!')
</script>

<template>
  <div>
    <input v-model="input" />
  </div>
</template>
Edit this page
Last Updated:
Contributors: 袁果锅
Prev
vue.js 下载文件.md
Next
vxe-table笔记.md