@vueuse/core 常用方法

2024-05-15 02:12
文章标签 方法 core 常用 vueuse

本文主要是介绍@vueuse/core 常用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

createGlobalState | VueUse中文文档 

<template><div class="container"><div>{{ x }}-{{ y }}</div><div>{{ store }}</div><div><button @click="addNum">add-{{ counter }}</button></div><div><button @click="fullScreen">全屏</button></div><div><button @click="copyContent">点击复制</button></div><div :class="mode"><button @click="btn">切换主题色</button></div><div><button ref="element">useEventListener</button></div><div>{{ width }}-{{ height }}</div><div><button @click="throttledFn">throttledFn 3s</button></div><div><input ref="target" type="text" /><button type="button" @click="focused = true">focus</button></div><div ref="outside" style="border: 1px solid #ccc">Hello world</div></div>
</template><script lang="ts" setup>
import { watch, ref } from "vue";
import {useFullscreen,useClipboard,useLocalStorage,useMouse,useWindowSize,useDebounceFn,useThrottleFn,useColorMode,useEventListener,useCloned,useFocus,onClickOutside,
} from "@vueuse/core";// 鼠标坐标
const { x, y } = useMouse();// clone
const original = ref({ key: "value" });
const { cloned } = useCloned(original);
original.value.key = "some new value";console.log(cloned.value, original.value);// useStorage, useLocalStorage, useSessionStorage
const store = useLocalStorage("my-storage", {name: "Apple",color: "red",
});// 防抖
const counter = ref(0);
const addNum = useDebounceFn(() => {counter.value += 1;},300,{ maxWait: 5000 }
);// 节流
const throttledFn = useThrottleFn(() => {// do something, it will be called at most 1 time per secondconsole.log("throttledFn");
}, 3000);// 全屏
const { isFullscreen, toggle } = useFullscreen();
const fullScreen = () => {console.log("isFullScreen=", isFullscreen.value);toggle();
};// 点击粘贴useClipboard
const { text, copy, isSupported } = useClipboard();
const copyContent = () => {console.log("当前浏览器是否支持", isSupported.value, text.value);copy("点击复制");
};// 切换颜色
const mode = useColorMode();
const btn = () => {mode.value = mode.value === "light" ? "dark" : "light";
};// vueuse事件绑定
const element = ref(null);
useEventListener(element, "click", () => {console.log("监听点击事件");
});// 获取窗口宽高
const { width, height } = useWindowSize();// 自动获取焦点
const target = ref(null);
const { focused } = useFocus(target, { initialValue: true });watch(focused, (focused) => {if (focused) console.log("input element has been focused");else console.log("input element has lost focus");
});// 在这个元素外点击触发
const outside = ref(null);
onClickOutside(outside, (event) => console.log("outside click=", event));
</script><style scoped>
.light {background-color: #ccc;
}
.dark {background-color: #333;
}
.container {padding: 10px;display: flex;justify-content: center;align-items: center;align-content: center;flex-wrap: wrap;flex-direction: column;
}.container > div {width: 400px;display: flex;justify-content: center;
}
</style>

这篇关于@vueuse/core 常用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/990517

相关文章

Python清空Word段落样式的三种方法

《Python清空Word段落样式的三种方法》:本文主要介绍如何用python-docx库清空Word段落样式,提供三种方法:设置为Normal样式、清除直接格式、创建新Normal样式,注意需重... 目录方法一:直接设置段落样式为"Normal"方法二:清除所有直接格式设置方法三:创建新的Normal样

在Linux系统上连接GitHub的方法步骤(适用2025年)

《在Linux系统上连接GitHub的方法步骤(适用2025年)》在2025年,使用Linux系统连接GitHub的推荐方式是通过SSH(SecureShell)协议进行身份验证,这种方式不仅安全,还... 目录步骤一:检查并安装 Git步骤二:生成 SSH 密钥步骤三:将 SSH 公钥添加到 github

把Python列表中的元素移动到开头的三种方法

《把Python列表中的元素移动到开头的三种方法》在Python编程中,我们经常需要对列表(list)进行操作,有时,我们希望将列表中的某个元素移动到最前面,使其成为第一项,本文给大家介绍了把Pyth... 目录一、查找删除插入法1. 找到元素的索引2. 移除元素3. 插入到列表开头二、使用列表切片(Lis

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

Java Thread中join方法使用举例详解

《JavaThread中join方法使用举例详解》JavaThread中join()方法主要是让调用改方法的thread完成run方法里面的东西后,在执行join()方法后面的代码,这篇文章主要介绍... 目录前言1.join()方法的定义和作用2.join()方法的三个重载版本3.join()方法的工作原

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分