Skip to content

QRCode 二维码 ​

uvx-qrcode 根据文本内容生成二维码,支持纠错等级、边距、颜色、圆角/圆点风格、中心图片以及组件实例导出和保存。

平台兼容性 ​

AndroidiOS鸿蒙H5微信
4.25+4.25+4.61+4.25+4.41+

小程序端仅支持微信。App 端使用原生视图绘制和 takeSnapshot 导出,不使用 canvasToTempFilePath。

平台能力 ​

能力AndroidiOS鸿蒙H5微信
二维码矩阵、纠错等级、颜色和边距√√√√√
make、remake、生成结果事件√√√√√
toTempFilePathtakeSnapshottakeSnapshottakeSnapshot受运行容器限制Canvas 导出
save 保存到系统相册√√√×√

使用须知

  1. value 为空或生成、导出、保存失败时触发 error 事件,参数为错误信息;失败时不会触发对应的成功事件。
  2. hide 只隐藏视觉内容并保留节点尺寸,适合导出;不要通过父级 v-if、display:none 或销毁组件隐藏。
  3. auto 默认为 true,监听 value、size 和 options 变化自动重绘;关闭后需调用 make 或 remake。
  4. App 端导出使用 UniElement.takeSnapshot,必须在组件已完成生成且节点仍挂载时调用。
  5. H5 不支持直接保存到系统相册。调用 save 后会生成临时图片,并根据 h5-save-tip 显示长按保存提示;图片准备完成时触发 save 事件。

基础用法 ​

vue
<template>
   <uvx-qrcode value="https://www.uvxui.cn"></uvx-qrcode>
</template>

自定义样式 ​

vue
<template>
   <uvx-qrcode
      value="https://www.uvxui.cn"
      :size="240"
      :options="options"
   ></uvx-qrcode>
</template>

<script lang="uts" setup>
import type { Options } from "@/uni_modules/uvx-ui/components/uvx-qrcode/types/index.uts";

// 二维码高级配置
const options = ref<Options>({
   margin: 8,
   errorCorrectLevel: "Q",
   style: "round",
   foregroundColor: "#3c9cff",
   backgroundColor: "#ffffff",
});
</script>

手动生成 ​

vue
<template>
   <uvx-qrcode
      ref="uQrcode"
      value="手动生成二维码"
      :auto="false"
      @complete="handleComplete"
      @error="handleError"
   ></uvx-qrcode>
   <uvx-button text="生成二维码" @click="makeQRCode"></uvx-button>
</template>

<script lang="uts" setup>
// 二维码组件实例
const uQrcode = ref<UvxQrcodeComponentPublicInstance | null>(null);

const makeQRCode = (): void => {
   uQrcode.value?.make();
};

const handleComplete = (): void => {
   uni.showToast({ title: "生成成功", icon: "success" });
};

const handleError = (message: string): void => {
   console.error(message);
   uni.showToast({ title: "生成失败", icon: "none" });
};
</script>

导出与保存 ​

vue
<template>
   <uvx-qrcode
      ref="uQrcode"
      value="https://www.uvxui.cn"
      @error="handleError"
      @export="handleExport"
      @save="handleSave"
   ></uvx-qrcode>
   <uvx-button text="导出二维码" @click="exportQRCode"></uvx-button>
   <uvx-button text="保存二维码" @click="saveQRCode"></uvx-button>
</template>

<script lang="uts" setup>
// 二维码组件实例
const uQrcode = ref<UvxQrcodeComponentPublicInstance | null>(null);

const exportQRCode = (): void => {
   uQrcode.value?.toTempFilePath();
};

const saveQRCode = (): void => {
   uQrcode.value?.save();
};

const handleExport = (tempFilePath: string): void => {
   console.log("导出路径", tempFilePath);
};

const handleSave = (_tempFilePath: string): void => {
   uni.showToast({ title: "保存成功", icon: "success" });
};

const handleError = (message: string): void => {
   console.error(message);
   uni.showToast({ title: "操作失败", icon: "none" });
};
</script>

API ​

Props ​

属性说明类型默认值平台
value二维码内容string | number""全部
options二维码高级配置Options{}全部
size组件尺寸,数字单位为 pxstring | number200全部
file-type导出格式"jpg" | "png""png"Web/微信
start挂载后自动生成booleantrue全部
auto内容变化后自动重绘booleantrue全部
hide隐藏视觉内容但保留导出节点booleanfalse全部
loading是否显示加载状态booleanfalse全部
h5-save-tip是否显示保存提示booleantrueH5
u-style根节点自定义样式,CSS 字符串string""全部
u-class根节点外部样式类string""全部

Options ​

以下字段会参与当前版本的矩阵生成或 Canvas 绘制。data 优先于 text,二者未提供有效内容时使用组件的 value。

字段说明类型默认值
data二维码内容,优先级最高string-
textdata 未设置时使用的二维码内容string-
typeNumber二维码版本,范围 1-40;小于 1 时自动选择number0
errorCorrectLevel纠错等级L | M | Q | HH
margin二维码内容边距,单位 pxnumber0
areaColor整个画布的底色;设置后内容区域使用 backgroundColorstring-
backgroundColor二维码背景色string#ffffff
foregroundColor二维码前景色string#000000
style码点样式square | round | dotsquare
foregroundImageSrc中心图片地址string-
foregroundImageWidth中心图片宽度,单位 pxnumber内容区域宽度的 1/4
foregroundImageHeight中心图片高度,单位 pxnumber内容区域高度的 1/4
foregroundImageX中心图片横坐标,单位 pxnumber水平居中
foregroundImageY中心图片纵坐标,单位 pxnumber垂直居中
foregroundImagePadding中心图片背景内边距,单位 pxnumber0
foregroundImageBackgroundColor中心图片背景色string#FFFFFF
foregroundImageBorderRadius中心图片圆角,单位 pxnumber0

Events ​

事件说明参数平台
click点击二维码画布UniEvent全部
changeremake 调用前触发-全部
complete二维码绘制成功-全部
error生成、导出或保存失败string,错误信息全部
export临时图片导出成功string,临时文件路径全部
save保存成功;H5 为临时图片准备完成string,临时文件路径全部

Slots ​

插槽说明参数
loading生成中内容-
h5saveH5 保存提示内容tempFilePath

Methods ​

方法说明结果事件
make()生成二维码complete / error
remake()触发 change 后重新生成change、complete / error
toTempFilePath()导出临时图片export / error
save()保存到系统相册;H5 显示长按保存提示save / error
getInstance()获取当前矩阵和配置快照返回 Instance

参考 ​

uvx-ui · uni-app x 组件库