首页 前端知识 @vueuse/core 常用方法

@vueuse/core 常用方法

2024-07-27 22:07:14 前端知识 前端哥 492 144 我要收藏

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 second
  console.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 常用方法,也会在我们的前端体系课中会讲的。不过我们讲的更生动,更具体,还有配套练习,可以让你短时间掌握前端的内容,带着你做前端的实战项目!

让你短期内拥有前端的项目经验!

如果在学习前端想要更快更高效,基础打的更牢固的话,想要找一个强力监督和针对性指导,可以找靠谱的一对一前端指导教学,线上就能灵活学习,4个月掌握前端技能,满足企业要求。让你学前端学的深入,学的高效,学的系统~

而且线上学习性价比高,学习方式灵活,学的快,不耽误平时工作,过渡比较平滑,转行学习无压力。无论是转行入门,还是在职提升进阶,都会个性化安排课表和课程,可以让你有非常大的收获和进步!

现在可以免费体验,点下面的链接进一步了解:

https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.1d293d0dn1JnVe&ft=t&id=807595438708&sku_properties=1627207:132476

转载请注明出处或者链接地址:https://www.qianduange.cn//article/14430.html
标签
评论
发布的文章

Unity数据持久化之Json

2024-08-10 22:08:00

simdjson 高性能JSON解析C 库

2024-08-10 22:08:00

npm常用命令详解(一)

2024-08-10 22:08:34

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!