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. Ext.js二级联动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link href ...

  2. python学习-- Django根据现有数据库,自动生成models模型文件

    Django引入外部数据库还是比较方便的,步骤如下 : 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自 ...

  3. [python学习篇] uiautomator xiaocong

    Skip to content     This repository Pull requests Issues Marketplace Gist   Sign out       Watch103 ...

  4. Invalid regular expression flags 错误

    找到写正则表达式的地方,检查是不是写了一个非法的正则表达式. Invalid regular expression flags

  5. 关于php ‘==’ 与 '===' 遇见的坑

    两个的区别所有PHPer都知道, 今天在遍历 xmlNode时,自己写的代码就碰坑了 想遍历xmlNode为数组 得到的xmlNode为 想要把所有的simpleXmlElement对象都遍历转成数组 ...

  6. Welcome-to-Swift-19类型嵌套(Nested Types)

    枚举类型常被用于实现特定类或结构体的功能.也能够在有多种变量类型的环境中,方便地定义通用类或结构体来使用,为了实现这种功能,Swift允许你定义类型嵌套,可以在枚举类型.类和结构体中定义支持嵌套的类型 ...

  7. setsockopt等高级使用

    参考: setsockopt函数使用http://hi.baidu.com/yelangdefendou/item/74161d0f384abd3c4ac4a316http://blog.csdn.n ...

  8. springboot中如果使用了@Autowired注入了bean,则这个类也要为spring bean,new出来注入的bean为null

    https://blog.csdn.net/Mr_Runner/article/details/83684088 问题:new出来的实例中含有@Autowired注入时,注入的Bean为null: 解 ...

  9. hdu 1277 AC自动机

    全文检索 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程

    sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386 \  libx11-dev:i386 ...