Skip to content

useBase64

Category
Export Size
769 B
Last Changed
15 minutes ago

响应式的 Base64 转换。 支持 plain text, buffer, files, canvas, objects, maps, sets 和 images.

Demo

文本输入
Base64
Buffer 输入
new ArrayBuffer(1024)
Base64
文件选择
Base64
图片输入
Base64

用法

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)

如果你使用对象、数组、Map或Set,你可以在选项中提供序列化器。否则,你的数据将使用默认的序列化器进行序列化。 对象和数组将通过 JSON.stringify 转换为字符串。Map和Set将在转换为对象和数组之后再进行字符串化。

类型声明

显示类型声明
typescript
export interface ToDataURLOptions {
  /**
   * MIME type
   */
  type?: string | undefined
  /**
   * jpeg 或 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

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
Mikhailov Nikita
wheat
jelf

变更日志

v10.0.0-beta.5 on 4/13/2023
cb644 - refactor!: remove isFunction and isString utils
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue

Released under the MIT License.