vue2.0 自定义 生成二维码(QRCode)组件
1.自定义 生成二维码组件
QRCode.vue
<!-- 生成二维码 组件 -->
<template>
<canvas
class="qrcode-canvas"
:class="{show: show}"
:style="{height: size + 'px', width: size + 'px'}"
:height="size"
:width="size"
ref="qr"
></canvas>
</template> <script>
import qr from 'qr.js'
export default {
name: 'qrcode',
props: {
val: {
type: String,
required: true
},
size: {
type: Number,
default: 200
},
// 'L', 'M', 'Q', 'H'
level: String,
bgColor: {
type: String,
default: '#FFFFFF'
},
fgColor: {
type: String,
default: '#000000'
},
show: {
type: Boolean,
default: true
}
},
watch: {
size: function(){
this.update()
},
val: function(){
this.update()
},
level: function(){
this.update()
},
bgColor: function(){
this.update()
},
fgColor: function(){
this.update()
}
},
mounted () {
this.update()
console.log(this.show)
},
methods:{
update () {
var size = this.size
var bgColor = this.bgColor
var fgColor = this.fgColor
var $qr = this.$refs.qr
var qrcode = qr(this.val)
var ctx = $qr.getContext('2d')
var cells = qrcode.modules
var tileW = size / cells.length
var tileH = size / cells.length
var scale = (window.devicePixelRatio || 1) / getBackingStorePixelRatio(ctx)
$qr.height = $qr.width = size * scale
ctx.scale(scale, scale)
cells.forEach(function (row, rdx) {
row.forEach(function (cell, cdx) {
ctx.fillStyle = cell ? fgColor : bgColor
var w = (Math.ceil((cdx + 1) * tileW) - Math.floor(cdx * tileW))
var h = (Math.ceil((rdx + 1) * tileH) - Math.floor(rdx * tileH))
ctx.fillRect(Math.round(cdx * tileW), Math.round(rdx * tileH), w, h)
})
})
}
}
}
function getBackingStorePixelRatio (ctx) {
return (
ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio ||
1
)
}
</script> <style lang="less" scoped>
.qrcode-canvas {
display: none
}
.show {
display: block;
}
</style>
2.页面调用
<!-- 生成二维码 -->
<template>
<div>
<!-- 标题栏 -->
<mt-header title="生成二维码">
<router-link to="/" slot="left">
<mt-button icon="back">返回</mt-button>
</router-link>
</mt-header>
<!-- 内容 -->
<div id="qrCode">
<QRCode :val="val" :show="true" />
</div>
<!-- 按钮 -->
<mt-button type="primary" @click="changeUrl">修改url</mt-button>
</div>
</template> <script>
import QRCode from '../components/QRCode.vue'
import { MessageBox } from 'mint-ui'; export default {
name: 'QR',
components: {
QRCode
},
data(){
return {
val:'https://www.baidu.com/s?wd=123'
}
},
methods: {
changeUrl(){
MessageBox.prompt('请输入新的url').then(({ value, action }) => {
this.val = value;
});
}
}
}
</script> <style>
/*垂直水平居中*/
#qrCode {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin: -100px 0 0 -100px;
}
.mint-button{
width: 80%;
margin: 20px auto;
display: block;
}
</style>
3.效果图

vue2.0 自定义 生成二维码(QRCode)组件的更多相关文章
- PHP自定义生成二维码跳转地址
比较简单的一款PHP自定义生成二维码跳转地址,手机端微信扫码,自动跳转到定义好的链接.支持自定义生成二维码尺寸.间距等. 鼠标悬浮显示二维码弹出层,离开后消失.js实现,代码如下: $(fu ...
- js生成二维码 qrcode
js生成二维码 QRcode npm 地址 1.安装qrcode //在项目文件夹中执行: npm install --save qrcode //或者,将其全局安装以使用qrcode命令行来保存qr ...
- Java生成二维码QRCode.jar
所需jar包:QRCode.jar:http://download.csdn.net/detail/xuxu198899223/7717745 package ewm; import java.awt ...
- 【C#/WPF】.Net生成二维码QRCode的工具
先马 http://qrcodenet.codeplex.com/ 使用该工具WPF生成二维码的简单例子: 前台XAML准备一个Image控件显示二维码. string qrcodeStr = &qu ...
- vue 生成 二维码 qrCode 插件 使用 方法
首先安装方法:(--save 参数会改变package.json 推荐使用 下次直接install就行了) npm install --save qrcode 然后项目使用: import QRCod ...
- 使用JS生成二维码QRCode
这其实很简单,项目中使用插件即可生成,主要有两种方式: 一种是在项目中使用java生成,把图片生成到某个目录,然后在用tomcat或者nginx虚拟一下路径即可访问,这种方式我们不用,因为会在目录中生 ...
- php 生成二维码(qrcode)
可以用composer安装 https://packagist.org/packages/endroid/qrcode
- .net 中生成二维码的组件
http://qrcodenet.codeplex.com/
- C# 利用QRCode生成二维码图片
网上生成二维码的组件是真多,可是真正好用的,并且生成速度很快的没几个,QRCode就是我在众多中找到的,它的生成速度快.但是网上关于它的使用说明,真的太少了,大都是千篇一律的复制粘贴.这是本要用它做了 ...
随机推荐
- python装饰器实现用户密码认证(简单初形)
import timecurrent_user={'user':None}def auth(engine = 'file'): def deco(func): #func=最初始的index和最初始的 ...
- springboot添加外部jar包及打包
项目中除了从pom中添加依赖包,还可以添加本地的jar包,怎么做呢? 1.在src下新建目录lib,将jar包添加到lib中 2.在pom文件里添加配置以下属性,就可以使用jar包了 <depe ...
- 关于windows系统DPI增大导致字体变大的原因分析
最近再学习WPF开发,其中提到一个特性“分辨率无关性”,主要功能就是实现开发的桌面程序在不同分辨率的电脑上显示时,会根据系统的DPI自动进行UI的缩放,从而不会导致应用程序的失真. 这个里面就提到了个 ...
- JDBC 学习笔记(七)—— CallableStatement
在大型关系型数据库中,有一组为了完成特定功能的 SQL 语句集被称为存储过程(Stored Procedure),它是数据库中的对象. JDBC 使用 CallableStatement 对象,完成对 ...
- P1266 速度限制 (最短路,图论)
题目链接 Solution 在最短路转移的时候在队列或者堆中记录状态为 \(f[u][v]\) 代表上一个节点为 \(u\) ,速度为 \(v\) . 然后按部就班转移即可... Code #incl ...
- [转] Makefile 基础 (10) —— Makefile 后序
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...
- Unity 过度光照贴图
背景:开关窗帘过程,让环境在亮和暗之间过度 事先烘培出亮.暗两张Lighting map.然后代码实现,窗帘开关由动作实现,而代码中通过动作执行进度来过度两张Lighting map void OnA ...
- 洛谷 [P1948] 电话线
二分答案 首先,最大值最小,就是二分答案 #include <iostream> #include <cstdio> #include <algorithm> #i ...
- UVa12333 Revenge of Fibonacci
高精度 trie 暴力预处理出前100000个fibonacci数,将每个数的前40位数字串插入到trie中,记录每个结点最早可以由哪个数字串到达. 然后依次回答询问即可. 存fibonacci数的数 ...
- net1:DateTime,Application与Session,
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...