使用vue-qr插件生成二维码

vue 使用 vue-qr 插件生成二维码并下载

vue-qr 官方 gitee 地址 https://www.npmjs.com/package/vue-qr

安装vue-qr插件

npm install vue-qr --save

引入vue-qr

js 复制代码
// vue2
import vueQr from 'vue-qr'
// vue3
import vueQr from 'vue-qr/src/packages/vue-qr.vue'

例子

html 复制代码
<template>
  <div style="text-align: center;">
    <vue-qr 
      :text="qrUrl"
      :callback="qrCallBack" 
      :size="200"
      :margin="10"
      colorDark="green" 
      colorLight="white" 
      logoSrc="/favicon.ico"
      :logoScale="0.2"
      :dotScale="0.7">
    </vue-qr>
    <div style="text-align: center;">
      <button @click="downloadQrcode">下载二维码</button>
    </div>
  </div>
</template>
js 复制代码
<script>
// 插件引入
import vueQr from 'vue-qr/src/packages/vue-qr.vue'

export default {
  name: 'App',
  components: { vueQr },
  data () {
    return {
      // 用于生成二维码的url
      qrUrl: 'https://www.npmjs.com/package/vue-qr',
      // 下载数据base64
      qrDownloadUrl: ''
    }
  },
  methods: {
    // 二维码刷新后调用
    qrCallBack(qrUrl) {
      this.qrDownloadUrl = qrUrl;
    },
    // 下载二维码
    downloadQrcode() {
      let a = document.createElement('a');
      a.download = '我的二维码';
      a.href = this.qrDownloadUrl;
      a.click();
    },
  }
}
</script>

效果图

属性说明

这个表格是从vue-qr官方文档中粘贴过来的

属性 说明
text Contents to encode. 欲编码的内容
correctLevel Correct Level 0-3 容错级别 0-3
size Width as well as the height of the output QR code, includes margin. 尺寸, 长宽一致, 包含外边距
margin Margin to add around the QR code, default 20px. 二维码图像的外边距, 默认 20px
colorDark Color of "true" blocks. Works only when both colorDark and colorLight are set. (BYTE_DTA, BYTE_POS, BYTE_AGN, BYTE_TMG) 实点的颜色
colorLight Color of empty space, or "false" blocks. Works only when both colorDark and colorLight are set. (BYTE_EPT) 空白区的颜色
components Controls the appearances of parts in the QR code. Read section ComponentOptions to learn more. 阅读 ComponentOptions 了解更多信息。
bgSrc Background url to embed in the QR code. 欲嵌入的背景图地址
gifBgSrc Gif background url to embed in the QR code, If gifBackground is set, backgroundImage will be ignored. This option will affects performance. 欲嵌入的背景图 gif 地址,设置后普通的背景图将失效。设置此选项会影响性能
backgroundColor Background color 背景色
backgroundDimming Color mask to add above the background image. Helpful when having problems with decoding. 叠加在背景图上的颜色, 在解码有难度的时有一定帮助
logoSrc Logo url to embed at the center of generated QR code 嵌入至二维码中心的 LOGO 地址
logoScale Value used to scale the logo image. Larger value may result in decode failure. Size of the logo equals to logoScale*(size-2*margin). Default is 0.2. 用于计算 LOGO 大小的值, 过大将导致解码失败, LOGO 尺寸计算公式 logoScale*(size-2*margin), 默认 0.2
logoMargin White margin that appears around the logo image. Default is 0. LOGO 标识周围的空白边框, 默认为0
logoBackgroundColor Logo background color, need set logo margin. Logo 背景色,需要设置 logo margin
logoCornerRadius Radius of the logo's corners.Default is 0 LOGO 标识及其边框的圆角半径, 默认为0
whiteMargin If set to true, a white border will appear around the background image. Default is true. 若设为 true, 背景图外将绘制白色边框
dotScale Value used to scale down the data dots' size. (0 < scale < 1.0) default 1 数据区域点缩小比例,默认为1
autoColor If set to true, the dominant color of backgroundImage will be used as colorDark. Default is true. 若为 true, 背景图的主要颜色将作为实点的颜色, 即 colorDark,默认 true
binarize If set to true, the whole image will be binarized with the given threshold, or default threshold if not specified. Default is false. 若为 true, 图像将被二值化处理, 未指定阈值则使用默认值
binarizeThreshold Threshold used to binarize the whole image. Default is 128. (0 < threshold < 255) 二值化处理的阈值
callback Data URI of the generated QR code will be available here. 生成的二维码 Data URI 可以在回调中取得,第一个参数为二维码 data URL, 第二个参数为 props 传过来的 qid(因为二维码生成是异步的,所以加个 id 用于排序)
bindElement If set to true, the generated QR will bind to a HTML element automatically. Default is true. 指定是否需要自动将生成的二维码绑定到HTML上, 默认是true
相关推荐
消失的旧时光-19437 分钟前
Flutter 工程中 mixin 的正确打开方式:5 种高质量设计范式 + mixin vs 继承 vs 组合 + 为什么它比 BasePage 更优雅
前端·flutter·架构
乐吾乐科技10 分钟前
乐吾乐3D可视化2025重大更新与2026升级计划
前端·3d·信息可视化·编辑器·数据可视化
C_心欲无痕11 分钟前
html - 使用视频做天气卡片背景
前端·html·音视频
毕设十刻29 分钟前
基于Vue的养老服务平台85123(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
青衫折扇30 分钟前
执行 npm 安装命令时,包被装到了 C 盘用户目录下,而非项目根目录
前端·npm·node.js
XiaoYu200235 分钟前
第2章 Nest.js入门
前端·ai编程·nestjs
2501_9462447840 分钟前
Flutter & OpenHarmony OA系统下拉刷新组件开发指南
开发语言·javascript·flutter
没事多睡觉66641 分钟前
零基础React + TypeScript 教程
前端·react.js·typescript
liliangcsdn41 分钟前
MySQL存储字节类数据的方案示例
java·前端·数据库
_Kayo_43 分钟前
React useState setState之后获取到的数据一直是初始值
前端·javascript·react.js