vue 使用qrcode生成二维码并可下载保存

  1. 安装qrcode
shell 复制代码
npm install qrcode --save
  1. 代码
html 复制代码
<template>
	<div style="display: flex; flex-direction: column; align-items: center; justify-content center;">
       <div>查看溯源码,<a id="saveLink" style="text-decoration:underline">点击保存</a></div>
       <canvas id="qrcode-canvas"></canvas>
     </div>
</template>
<script>
import QRCode from 'qrcode'
export default {
	return data(){
		url: "", //待生成二维码的data
	},
	mounted(){
		generateQRCode(this.url)
	}
}

generateQRCode(url) {
  const canvas = document.getElementById('qrcode-canvas') // 在模板中的canvas元素设置id为 qrcode-canvas
  QRCode.toCanvas(canvas, url, {width:120,height:120})
  var dataURL = canvas.toDataURL("image/png")
  
  const a = document.getElementById("saveLink")
  a.href = dataURL
  a.download = `二维码.png` // 报文到本地时的文件名称
}
</script>
  1. 演示
相关推荐
一 乐2 分钟前
医疗管理|医院医疗管理系统|基于springboot+vue医疗管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·医疗管理系统
光影少年5 分钟前
React Native第六章
javascript·react native·react.js
晓得迷路了8 分钟前
栗子前端技术周刊第 105 期 - npm 安全性加强、Storybook 10、htmx 4.0 Alpha 1...
前端·javascript·npm
七号练习生.c26 分钟前
CSS入门
前端·css·tensorflow
程序员爱钓鱼26 分钟前
Python编程实战——Python实用工具与库:Matplotlib数据可视化
前端·后端·python
程序员爱钓鱼30 分钟前
Python编程实战 - Python实用工具与库 - requests 与 BeautifulSoup
前端·后端·python
wyzqhhhh31 分钟前
前端跨页面通信
前端
G018_star sky♬36 分钟前
原生JavaScript实现输入验证的界面
javascript·css·css3
火龙谷40 分钟前
DrissionPage遇到iframe
开发语言·前端·javascript