Skip to content

useBase64

反应式 base64 转换。支持纯文本、缓冲区、文件、画布、对象、映射、集和图片。

¥Reactive base64 transforming. Supports plain text, buffer, files, canvas, objects, maps, sets and images.

示例

Text Input
Base64
Buffer Input
new ArrayBuffer(1024)
Base64
File Input
Base64
Image Input
Base64

用法

¥Usage

ts
import { useBase64 } from '@vueuse/core'
import { Ref, ref } from 'vue'

const text = ref('')

const { base64 } = useBase64(text)
js
import { useBase64 } from '@vueuse/core'
import { ref } from 'vue'
const text = ref('')
const { base64 } = useBase64(text)

如果你使用对象、数组、映射或集合,你可以在选项中提供序列化器。否则,你的数据将由默认序列化器序列化。对象和数组将通过 JSON.stringify 转换为字符串。 =在 stringify 之前,Map 和 Set 会分别转换为对象和数组。

¥If you use object, array, map or set you can provide serializer in options. Otherwise, your data will be serialized by default serializer. Objects and arrays will be transformed in string by JSON.stringify. Map and set will be transformed in object and array respectively before stringify.

类型声明

显示类型声明
typescript
export interface ToDataURLOptions {
  /**
   * MIME type
   */
  type?: string | undefined
  /**
   * Image quality of jpeg or webp
   */
  quality?: any
}
export interface UseBase64ObjectOptions<T> {
  serializer: (v: T) => string
}
export interface UseBase64Return {
  base64: Ref<string>
  promise: Ref<Promise<string>>
  execute: () => Promise<string>
}
export declare function useBase64(
  target: MaybeRefOrGetter<string>,
): UseBase64Return
export declare function useBase64(
  target: MaybeRefOrGetter<Blob>,
): UseBase64Return
export declare function useBase64(
  target: MaybeRefOrGetter<ArrayBuffer>,
): UseBase64Return
export declare function useBase64(
  target: MaybeRefOrGetter<HTMLCanvasElement>,
  options?: ToDataURLOptions,
): UseBase64Return
export declare function useBase64(
  target: MaybeRefOrGetter<HTMLImageElement>,
  options?: ToDataURLOptions,
): UseBase64Return
export declare function useBase64<T extends Record<string, unknown>>(
  target: MaybeRefOrGetter<T>,
  options?: UseBase64ObjectOptions<T>,
): UseBase64Return
export declare function useBase64<T extends Map<string, unknown>>(
  target: MaybeRefOrGetter<T>,
  options?: UseBase64ObjectOptions<T>,
): UseBase64Return
export declare function useBase64<T extends Set<unknown>>(
  target: MaybeRefOrGetter<T>,
  options?: UseBase64ObjectOptions<T>,
): UseBase64Return
export declare function useBase64<T>(
  target: MaybeRefOrGetter<T[]>,
  options?: UseBase64ObjectOptions<T[]>,
): UseBase64Return

源代码

源代码示例文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号