javaScript系列笔记 - JS生成二维码

一个简单的 二维码生成实例:

1、需引用 qrcode.js,官网下载即可

qrcode.js

2、html

<div class="container-row explain-box" style="height: 2.3rem">
	<div class="">
		<div id="qrcode"></div>
		<img id="pic" style="height: 2.5rem;width: 2.5rem;"/>
	</div>
</div>

3、js

(function(){
    //qrcode  为div 属性id
    new QRCode(document.getElementById("qrcode"), {
    	text: "二维码内容",
	    width: 250,
    	height: 250,
	    colorDark : "#000000",
    	colorLight : "#ffffff",
	    correctLevel : QRCode.CorrectLevel.H
    });

})();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uni-app中生成二维码可以使用第三方库qrcode.js。以下是生成二维码的步骤: 1. 首先,通过npm将qrcode.js安装到你的uni-app项目中。打开命令行工具,切换到你的项目根目录下,执行以下命令: ```shell npm install qrcode.js --save ``` 2. 在需要生成二维码的页面中引入qrcode.js。打开你需要使用的页面的vue文件,添加以下代码: ```javascript import QRCode from 'qrcodejs2' export default { mounted() { // 在mounted钩子函数中调用生成二维码的方法 this.generateQRCode() }, methods: { generateQRCode() { // 获取二维码容器DOM元素 const qrcodeContainer = this.$refs.qrcodeContainer // 创建一个qrcode实例 const qrcode = new QRCode(qrcodeContainer, { text: 'https://www.csdn.net', // 二维码的内容 width: 200, // 二维码的宽度 height: 200 // 二维码的高度 }) // 或者你也可以通过动态数据传递给二维码的内容 // const qrcode = new QRCode(qrcodeContainer, { // text: this.dynamicData, // width: 200, // height: 200 // }) // 可以根据需要自定义二维码的样式 qrcodeContainer.children[0].style.margin = '10px' } } } ``` 3. 在页面的模板中添加一个用于渲染二维码的容器。在你的页面模板中,添加以下代码: ```html <template> <view> <canvas ref="qrcodeContainer"></canvas> </view> </template> ``` 这样,当你访问该页面时,就会生成一个指定内容的二维码并渲染在页面上。 以上就是在uni-app中生成二维码的方法,希望能帮到你!如果有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值