编程 前端项目中使用element-ui和qrcodejs2插件生成二维码并提供下载功能

2024-11-18 21:07:04 +0800 CST views 1027

前端实现生成二维码,提供下载功能

在项目中可以通过element-ui结合qrcodejs2插件实现二维码的生成,并且提供下载功能。本文将展示如何在弹窗中生成二维码并提供下载的功能。

安装插件

首先需要安装qrcodejs2插件:

yarn add qrcodejs2

项目引入

在项目中引入qrcodejs2库:

import QRCode from 'qrcodejs2';

示例代码

HTML部分

<el-table-column label="管理端卡号" align="center" prop="cardAdminNo" width="180">
  <template slot-scope="scope">
    <el-button size="mini" type="text" @click="onAdminClick(scope.row)">
      {{scope.row.cardNo}}
    </el-button>
  </template>
</el-table-column>

<el-dialog title="二维码" :visible.sync="show" width="300px">
  <div class="qrcode" ref="qrcode"></div>
  <span slot="footer" class="dialog-footer">
    <el-button @click="cancelDownload">取消</el-button>
    <el-button type="primary" @click="downloadCode">下载二维码</el-button>
  </span>
</el-dialog>

JS部分

import QRCode from 'qrcodejs2';

export default {
  data() {
    return {
      show: false,
      title: '',
      qrcodeTitle: '',
      qrcode: null,
    };
  },
  methods: {
    onAdminClick(row) {
      const no = row.uniqueCode;
      const url = 'https://www.XXX.cn/admin.html?iid=' + no;
      this.show = true;
      this.title = '管理端二维码';
      this.qrcodeTitle = '管理端二维码 ' + row.cardAdminNo;

      if (this.$refs.qrcode) {
        this.$refs.qrcode.innerHTML = ""; // 清空二维码,避免重复生成
      }

      this.$nextTick(() => {
        this.qrcode = new QRCode(this.$refs.qrcode, {
          text: url,
          width: 200,
          height: 200,
        });
      });
    },
    downloadCode() {
      const nodeList = Array.prototype.slice.call(this.qrcode._el.children);
      const img = nodeList.find(item => item.nodeName.toUpperCase() === 'IMG'); // 获取二维码图片

      // 构建画布
      const canvas = document.createElement('canvas');
      canvas.width = img.width;
      canvas.height = img.height;
      canvas.getContext('2d').drawImage(img, 0, 0);

      // 生成下载链接
      const url = canvas.toDataURL('image/png');
      const a = document.createElement("a");
      a.href = url;
      a.download = this.qrcodeTitle + `.png`;
      a.click(); // 触发下载
    },
    cancelDownload() {
      this.show = false;
      this.$refs.qrcode.innerHTML = ""; // 清空二维码,避免生成多个
    },
  },
};

样式部分

<style lang="scss">
  .qrcode {
    justify-content: center;
    align-items: center;
    display: flex;
  }
  .qrcode img {
    width: 200px;
    height: 200px;
  }
</style>

代码解释

  1. 生成二维码

    • 点击按钮后,调用onAdminClick方法,生成一个基于唯一编码(uniqueCode)的二维码。
    • 使用QRCode库生成二维码,并通过$nextTick确保在DOM更新完成后插入二维码。
  2. 下载二维码

    • 通过downloadCode方法获取生成的二维码图像(IMG),并绘制在canvas上。
    • canvas转换为图片URL,使用<a>标签的download属性触发下载。
  3. 取消二维码显示

    • 点击取消按钮调用cancelDownload方法,隐藏弹窗并清空二维码。

小结

通过qrcodejs2结合element-ui的弹窗组件,我们可以很方便地在前端实现二维码生成和下载功能。如果你想在你的项目中实现类似功能,以上代码可以作为参考模板。

推荐文章

四舍五入五成双
2024-11-17 05:01:29 +0800 CST
2024年公司官方网站建设费用解析
2024-11-18 20:21:19 +0800 CST
Vue3中如何进行性能优化?
2024-11-17 22:52:59 +0800 CST
OpenCV 检测与跟踪移动物体
2024-11-18 15:27:01 +0800 CST
五个有趣且实用的Python实例
2024-11-19 07:32:35 +0800 CST
Vue3 中提供了哪些新的指令
2024-11-19 01:48:20 +0800 CST
2025,重新认识 HTML!
2025-02-07 14:40:00 +0800 CST
Redis和Memcached有什么区别?
2024-11-18 17:57:13 +0800 CST
什么是Vue实例(Vue Instance)?
2024-11-19 06:04:20 +0800 CST
Vue3中如何实现响应式数据?
2024-11-18 10:15:48 +0800 CST
使用xshell上传和下载文件
2024-11-18 12:55:11 +0800 CST
虚拟DOM渲染器的内部机制
2024-11-19 06:49:23 +0800 CST
防止 macOS 生成 .DS_Store 文件
2024-11-19 07:39:27 +0800 CST
120个实用CSS技巧汇总合集
2025-06-23 13:19:55 +0800 CST
程序员出海搞钱工具库
2024-11-18 22:16:19 +0800 CST
初学者的 Rust Web 开发指南
2024-11-18 10:51:35 +0800 CST
Rust 中的所有权机制
2024-11-18 20:54:50 +0800 CST
使用 sync.Pool 优化 Go 程序性能
2024-11-19 05:56:51 +0800 CST
Nginx 反向代理 Redis 服务
2024-11-19 09:41:21 +0800 CST
Golang在整洁架构中优雅使用事务
2024-11-18 19:26:04 +0800 CST
程序员茄子在线接单