vueuse的部分用法解析

2022-12-28  本文已影响0人  焚心123

npm i @vueuse/core

先介绍useStorage这个是将数据存储到本地内存中localStorage
import { useStorage } from "@vueuse/core"
const theDefault = {
  name: "Banana",
  color: "Yellow",
  size: "Medium",
  count: 0,
}
let state = useStorage("vue-use-local-storage", theDefault)
// state = JSON.parse(state.value)
console.log(state)
useFullscreen用于浏览器的全屏展示作用
//isFullscreen 当前是否是全屏,true/false,可用于一些逻辑的判断
//toggle  是函数直接调用即可
const { isFullscreen, toggle } = useFullscreen();

<button @click="toggle">点击切换</button>
useClipboard 剪切板功能
<script setup>
import { useClipboard } from "@vueuse/core"
//text 复制的内容
//copy 是复制的函数
// isSupported 判断当前的浏览器是否支持这个api
const input = ref("")
const { text, copy, isSupported } = useClipboard()
console.log(text, copy, isSupported)
</script>

<template>
  <div>
    <input v-model="input" type="text" />
    <button @click="copy(input)">Copy</button>
  </div>
</template>
useColorMode切换主题颜色(换肤)
<script setup>
import { useColorMode } from "@vueuse/core"
const mode = useColorMode() 

const btn = () => {
  mode.value = mode.value === "light" ? "dark" : "light"
}
</script>

<template>
  <div class="home" style="height: 100%">
    <button @click="btn">切换</button>
  </div>
</template>
<script setup>
import { useColorMode, useCycleList } from "@vueuse/core"
const mode = useColorMode({
  emitAuto: false,
  modes: {
    contrast: "dark contrast",
    cafe: "cafe",
  },
})
const { next } = useCycleList(["dark", "light", "cafe", "contrast", "auto"], {
  initialValue: mode,
})
</script>

<template>
  <button @click="next()">
    <i
      v-if="mode === 'dark'"
      i-carbon-moon
      inline-block
      align-middle
      class="align-middle"
    />
    <i
      v-if="mode === 'light'"
      i-carbon-sun
      inline-block
      align-middle
      class="align-middle"
    />
    <i
      v-if="mode === 'cafe'"
      i-carbon-cafe
      inline-block
      align-middle
      class="align-middle"
    />
    <i
      v-if="mode === 'contrast'"
      i-carbon-contrast
      inline-block
      align-middle
      class="align-middle"
    />
    <i
      v-if="mode === 'auto'"
      i-carbon-laptop
      inline-block
      align-middle
      class="align-middle"
    />

    <span class="ml-2 capitalize">{{ mode }}</span>
  </button>

  <span class="p-4 opacity-50">← Click to change the color mode</span>
</template>

<style>
html.cafe {
  filter: sepia(0.9) hue-rotate(315deg) brightness(0.9);
}
html.contrast {
  filter: contrast(2);
}
</style>
const a = ref("")
const { next } = useCycleList(["dark", "light", "cafe", "contrast", "auto"], {
  initialValue: a,
})
<button @click="next()">点击</button>------  {{ a }}
useDark开启暗黑模式
<script setup>
// import { useToggle } from "@vueuse/shared"
import { useDark, useToggle } from "@vueuse/core"
const isDark = useDark()
const toggleDark = useToggle(isDark)
</script>

<template>
  <button @click="toggleDark()">
   切换
  </button>
</template>

const isDark = useDark({
selector: "html", //这个是将当前的dark类显示的dom元素标签名
attribute: "color-scheme", //这个是class改为了color-scheme,不过一般使用class
valueDark: "dark",///暗黑class名字
valueLight: "light",////高亮class名字
})

useTitle动态修改标题名
<script setup>
// import { useToggle } from "@vueuse/shared"
import { useTitle } from "@vueuse/core"
const title = useTitle()
console.log(title.value) // print current title
const num = ref(0)
const btn = () => {
  num.value++
  title.value = "Hello" + num.value // change current title
}
</script>

<template>
  <button @click="btn">点击</button>
</template>

useCssVar用js操作css变量的
全局声明变量
:root {
  --color: red;
}

在其他页面中修改
<script setup lang="ts">
import { ref } from "vue"
import { useCssVar } from "@vueuse/core"
const el = ref(null)
const color = useCssVar("--color", el)
const switchColor = () => {
  color.value = "#df8543"
}
</script>

<template>
  <div ref="el" style="color: var(--color)">Sample text, {{ color }}</div>
  <button @click="switchColor">Change Color</button>
</template>
useEventListener监听事件
<script setup>
import { useEventListener } from "@vueuse/core"

const element = ref(null)
const cloup = useEventListener(element, "click", () => {
  console.log("监听点击事件")
})
cloup() //主动删除监听事件之后,不用在调用监听事件
</script>

<template>
  <div ref="element" >Sample text</div>
</template>

useEyeDropper滴管也叫取色器
<script setup>
import { useEyeDropper } from "@vueuse/core"

const { isSupported, open, sRGBHex } = useEyeDropper()
</script>

<template>
  <button @click="open" :disabled="!isSupported">
    滴管-----------{{ sRGBHex }}
  </button>
</template>
useWindowSize 动态的监听当前页面的大小
import { useWindowSize } from '@vueuse/core'

const { width, height } = useWindowSize()

useArrayFilter数组的方法,过滤
<script setup>
import { useArrayFilter } from "@vueuse/core"
import { reactive } from "vue"

let data = reactive({
  list1: [{ num: 1 }, { num: 2 }, { num: 3 }],
})
const result0 = computed(() => data.list1.filter((item) => item.num > 2))
data.list1.push({ num: 4 })
console.log(result0.value)

const list = [{ num: 1 }, { num: 2 }, { num: 3 }]
const result = useArrayFilter(list, (i) => i.num > 2)
list.push({ num: 4 })
console.log(result.value);
</script>
时间的两个api
<script setup>
import { useNow, useDateFormat } from "@vueuse/core"

const formatted = useDateFormat(useNow(), "YYYY-MM-DD HH:mm:ss")
</script>

<template>
  <div>{{ formatted }}</div>
</template>`
useBase64可以将文本内容,buffer,图片转为base64
import { useBase64 } from '@vueuse/core'

const text = ref('')

const { base64 } = useBase64(text)
useCounter数字累加器
<script setup>
import { useCounter } from "@vueuse/core"

const { count, inc, dec, set, reset } = useCounter(1, { min: 0, max: 16 })
</script>

<template>
  <div>{{ count }}</div>
  <button @click="inc()">加</button>
  <button @click="dec()">减</button>
  <button @click="set(10)">设置当前数值为10</button>
  <button @click="reset()">重置</button>
</template>
useCycleList循环列表,上面有提到过,在主题颜色哪里
<script setup>
import { useCycleList } from "@vueuse/core"

const { state, next, prev } = useCycleList([
  "Dog",
  "Cat",
  "Lizard",
  "Shark",
  "Whale",
  "Dolphin",
  "Octopus",
  "Seal",
])
</script>

<template>
  <div>{{ state }}</div>
  <button @click="next()">下一个</button>
  <button @click="prev()">上一个</button>
</template>
useToggle 切换
<script setup>
import { useToggle } from "@vueuse/core"
const value = ref(false)
// const [value, toggle] = useToggle()
const toggle = useToggle(value)
</script>

<template>
  <mark>{{ value }}</mark>
  <button @click="toggle()">切换</button>
</template>

####### useToNumber将字符串转化为数字类型

import { useToNumber } from '@vueuse/core'

const str = ref('123')
const number = useToNumber(str)

number.value // 123

useToString 将数字类型转化为字符串
import { useToString } from '@vueuse/core'

const number = ref(3.14)
const str = useToString(number)

str.value // '3.14'

####### useQRCode二维码生成器

npm i qrcode

<script setup>
import { useQRCode } from "../utils/useQrcode"
let inp = ref("")
let qrcode = null
const inpBtn = () => {
  qrcode = useQRCode(inp.value)
}
</script>

<template>
  <input type="text" v-model="inp" @input="inpBtn" />
  <img :src="qrcode" alt="qrcode" />
</template>

import { isClient, resolveRef } from "@vueuse/shared"
import QRCode from "qrcode"

/**
 * Wrapper for qrcode.
 *
 * @see https://vueuse.org/useQRCode
 * @param text
 * @param options
 */
export function useQRCode(text, options) {
  const src = resolveRef(text)
  const result = ref("")

  watch(
    src,
    async (value) => {
      console.log(src.value, value)
      if (src.value && isClient)
        result.value = await QRCode.toDataURL(value, options)
    },
    { immediate: true }
  )

  return result
}

上一篇下一篇

猜你喜欢

热点阅读