美文网首页Vue.js专区
vue.js中使用qrcode生成二维码

vue.js中使用qrcode生成二维码

作者: wallimn | 来源:发表于2019-08-25 22:28 被阅读0次

一、安装包

npm install qrcodejs2 --save

二、应用

<!--写在vue对应的元素里-->  
<div class="qrcode" ref="qrcodeContainer"></div>  
  
<script>  
import QRCode from 'qrcodejs2'  
// vue对象的一个method  
showQRCode(){  
    var qrcode = new QRCode(this.$refs.qrcodeContainer, {  
        text: 'https://wallimn.iteye.com',  
        width: 100,  
        height: 100,  
        colorDark: '#000000',  
        colorLight: '#ffffff',  
        correctLevel: QRCode.CorrectLevel.H  
    });  
}  
</script>  

三、注意

如果使用对话框显示二维码,有时会由于html元素还没有创建,导致生成二维码时报对象不存在的错误。这时可以使用nextTick来处理。

showQRCode(){  
    this.$nextTick(()=>{  
        var qrcode = new QRCode(this.$refs.qrcodeContainer, {  
            text: 'https://wallimn.iteye.com',  
            width: 100,  
            height: 100,  
            colorDark: '#000000',  
            colorLight: '#ffffff',  
            correctLevel: QRCode.CorrectLevel.H  
        })  
    }  
} 

相关文章

网友评论

    本文标题:vue.js中使用qrcode生成二维码

    本文链接:https://www.haomeiwen.com/subject/putlectx.html