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)组件的更多相关文章

  1. PHP自定义生成二维码跳转地址

      比较简单的一款PHP自定义生成二维码跳转地址,手机端微信扫码,自动跳转到定义好的链接.支持自定义生成二维码尺寸.间距等.    鼠标悬浮显示二维码弹出层,离开后消失.js实现,代码如下: $(fu ...

  2. js生成二维码 qrcode

    js生成二维码 QRcode npm 地址 1.安装qrcode //在项目文件夹中执行: npm install --save qrcode //或者,将其全局安装以使用qrcode命令行来保存qr ...

  3. Java生成二维码QRCode.jar

    所需jar包:QRCode.jar:http://download.csdn.net/detail/xuxu198899223/7717745 package ewm; import java.awt ...

  4. 【C#/WPF】.Net生成二维码QRCode的工具

    先马 http://qrcodenet.codeplex.com/ 使用该工具WPF生成二维码的简单例子: 前台XAML准备一个Image控件显示二维码. string qrcodeStr = &qu ...

  5. vue 生成 二维码 qrCode 插件 使用 方法

    首先安装方法:(--save 参数会改变package.json 推荐使用 下次直接install就行了) npm install --save qrcode 然后项目使用: import QRCod ...

  6. 使用JS生成二维码QRCode

    这其实很简单,项目中使用插件即可生成,主要有两种方式: 一种是在项目中使用java生成,把图片生成到某个目录,然后在用tomcat或者nginx虚拟一下路径即可访问,这种方式我们不用,因为会在目录中生 ...

  7. php 生成二维码(qrcode)

    可以用composer安装 https://packagist.org/packages/endroid/qrcode

  8. .net 中生成二维码的组件

    http://qrcodenet.codeplex.com/

  9. C# 利用QRCode生成二维码图片

    网上生成二维码的组件是真多,可是真正好用的,并且生成速度很快的没几个,QRCode就是我在众多中找到的,它的生成速度快.但是网上关于它的使用说明,真的太少了,大都是千篇一律的复制粘贴.这是本要用它做了 ...

随机推荐

  1. 大数据学习——sparkRDD

    https://www.cnblogs.com/qingyunzong/p/8899715.html 练习1:map.filter //通过并行化生成rdd val rdd1 = sc.paralle ...

  2. day04_07 while循环01

    while循环结构: #while 条件: print("any") print("any") 死循环案例 num = 1 while num<=10 : ...

  3. Clarke and five-pointed star

    Clarke is a patient with multiple personality disorder. One day, Clarke turned into a learner of geo ...

  4. ubuntu检测到系统错误解决方法

    解决方案: 1.打开终端,输入 sudo gedit /etc/default/apport 2.把里面的enabled=1改成enabled=0,保存

  5. c++中set容器的功能及应用。

    set的特性是,所有元素都会根据元素的键值自动排序(默认为升序),set中不允许两个元素有相同的键值. set基本操作: 1.头文件 #include<set>. 注:一定要加上using ...

  6. Python3网络爬虫(三):urllib.error异常

    运行平台:Windows Python版本:Python3.x IDE:Sublime text3 转载请注明作者和出处:http://blog.csdn.net/c406495762/article ...

  7. HDU1877 又一版 A+B

    Problem Description 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数.   Input 输入格式:测 ...

  8. Codeforces963B - Destruction of a Tree

    Portal Description 给出一个\(n(n\leq2\times10^5)\)个点的树,每次可以删除一个度数为偶数的点及其相连的边,求一种能够删掉整棵树的方案. Solution 简单起 ...

  9. 刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)

    题目: Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There ar ...

  10. Thrift & RPC介绍

    在学习thrift之前,先来看一下什么是rpc rpc远程过程调用,通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC采用客户机/服务器模式.请求程序就是一个客户机,而服务提供 ...